program wont compile for some reason?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #include <iostream>
#include <cmath>
using namespace std;

int main ()
{
int numValue;
int sum = 0;
int Value;

cin >> numValue;
cout << numValue << " VALUES READ" << endl;

for (int Value < 0;)
{ sum += Value;
}

return 0;
}


this is the error i keep getting :

test.cpp: In function `int main()':
test.cpp:14: parse error before `;' token

and this is what the program is supposed to do: Read in the number of input values. Write a
for loop to read each value and add it to a sum, only if
the value is negative.
The syntax for your for loop is wrong. See this page:
http://cplusplus.com/doc/tutorial/control/#for
thanks for the tip. i got it to compile but its not quite working how i need it to though. is their a statement im missing?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 #include <iostream>
using namespace std;

int main ()
{
int numValue;
int sum;
int Value;

cin >> numValue;
cout << numValue << " VALUES READ" << endl;

for (int numValue = numValue; Value < 0; )
{
cin >> Value;
sum += Value;
}

return 0;
}
im not sure what your doing with the current code. if you want to do what you are saying do something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
// by ui uiho

int main(){
int total = 0, value, cycle;
std::cout << "enter the number of numbers you will enter: ";
std::cin >> cycle;
while(cycle != 0){
std::cin >> value;
total += value;
--cycle;
}
std::cout << "your total is "<< total;
return 0; 
}


my guess is your problem is that you did not declare the value of sum, which means its value is in the memory it allocated, no matter if the data is actually a webpage, part of a picture, an mp3 file, you can not know what the value is. so as i did when i declared it, set its value to 0 so you know the value of the variable. c++ does not default variable values to 0, there are languages that do this.
You do have a few things wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <iostream>
using namespace std;

int main ()
{
int numValue , x; // We'll use x in the for loop
int sum = 0;// Give it a beginning value
int Value;

cin >> numValue;
cout << numValue << " VALUES READ" << endl;

for (x = 0; x < numValue; x++ )
{
cin >> Value;
sum += Value; // Now sum will be sum + Value inputted
}

cout << "sum is equal to " << sum << endl; // Add the statement to print the variable 'sum'
                                                                     // otherwise, why run the program?
return 0;
}
if you are using x just for the loop you should declare it in the scope of the loop, because that is the only place you are using it.

for ( int x = 0; x < numValue; x++){
// do ur sutff
}
Topic archived. No new replies allowed.