我开始真正地擅长编程,但是后来我一直讨厌这门课程:数组。我从来没有完全理解过C ++中的数组,这让我非常困惑。我有一个非常简单的程序,对于我做错的事情我只需要一点帮助。
到目前为止,这是我的代码:
#include <stdio.h>
#include <stdlib.h>
main() {
int num[50];
int i;
for (i = 0; i < 50; i++) {
printf("Enter a number (-999 to quit)\n ");
scanf("%i", &num[i]);
if (num == -999) {
printf("you chose to quit\n ");
}
}
printf("The numbers you entered are %i \n", num);
system("pause");
}
我的问题是:
-999为什么不能正常工作?在以前的程序中,我只使用了while (num != -999)
,它工作得很好,但是在这种情况下似乎也不起作用。
为什么阵列打印不正确?
请让我知道我在做错什么。
答案 0 :(得分:1)
if (num == -999)
这会将num
的基地址与值-999
进行比较,该值被解释为地址。不是预期的行为。检查num[i]
而不是num
。
printf("The numbers you entered are %i \n", num);
%i
说明符用于单个int
,但num
是它们的数组。更改为:
printf("The numbers you entered are:\n");
for (int i = 0; i < n; ++i) {
if (num[i] == -999)
break;
printf("%i\n", num[i]);
}
其中n
是num
中元素的数量(可以小于50)。
一个建议:由于这是C ++代码,所以最好使用cout
和C ++库而不是printf
和C库。
答案 1 :(得分:1)
由于您仍然声称(或被告知)您所编写的内容是C ++,因此以下是C ++的示例:
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
int main()
{
std::cout << "Gimme Numbers! Just enter something thats not a number to end.\n";
std::vector<int> numbers{ std::istream_iterator<int>{ std::cin },
std::istream_iterator<int>{} };
std::cout << "\nYa << " << numbers.size() << " Numbas:\n";
std::copy(std::begin(numbers), std::end(numbers),
std::ostream_iterator<int>{ std::cout, "\n" });
}
Gimme Numbers! Just enter something thats not a number to end.
15
45
97
8545
4654
5454
4564
54654
end
Ya 8 Numbas:
15
45
97
8545
4654
5454
4564
54654
现在与person
而不是int
相同。请注意,在缺少用于输出的字符串和类型main()
而不是person
的情况下,int
是如何保持不变的:
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <string>
struct person {
std::string name;
int age;
};
std::istream& operator>>(std::istream &is, person &p)
{
std::string name;
if (!(is >> name) || name == ".") {
is.setstate(std::ios::failbit);
return is;
}
int age;
if (is >> age)
p = { name, age };
return is;
}
std::ostream& operator<<(std::ostream &os, person const &p)
{
return os << p.name << ", age " << p.age;
}
int main()
{
std::cout << "Gimme names and their ages! End with \".\"\n";
std::vector<person> persons{ std::istream_iterator<person>{ std::cin },
std::istream_iterator<person>{} };
std::cout << "\nYa " << persons.size() << " buddies:\n";
std::copy(std::begin(persons), std::end(persons),
std::ostream_iterator<person>{ std::cout, "\n" });
}
Gimme names and their ages! End with "."
Monica 45
Carl 35
Lydia 23
Alex 89
.
Ya 4 buddies:
Monica, age 45
Carl, age 35
Lydia, age 23
Alex, age 89
答案 2 :(得分:0)
要使其工作,必须提供一个break语句退出for循环。
您还需要将数字读入阵列的插槽中,然后检查该插槽而不是整个阵列,以查看新插槽是否包含-999
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num[50];
int i;
for (i = 0; i < 50; i++) {
printf("Enter a number (-999 to quit)\n ");
// Scan the input into an integer slot in your array
scanf("%i", &num[i]);
// Check the slot to see if you an exit condition
if (num[i] == -999) {
printf("you chose to quit\n ");
break; // you have to exit the for loop by
// issuing a 'break;' after you get the -999
}
}
int numbers_read = i;
// print out the array -- Loop through all of the numbers
// using a for loop and an index, up to the number that were read in:
printf("The numbers you entered are: \n");
for(int j= 0; j < numbers_read; j++) {
printf("%i \n", num[j]);
}
system("pause");
}