#include <iostream>
#include <cmath>
usingnamespace 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.
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.
#include <iostream>
usingnamespace 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;
}