Help with building a code....
i have this problem:
Write a fragment of code that reads in a header value from the standard input and then reads in that many integers (also from standard input) and prints their sum to standard output (for loops, header values, accumulators, basic arithmetic). Declare any variables you use.
and this is my code:
int num, num2, num3;
cin >> num;
num2 = 0;
for(int i = 0; i < num; i++)
{
cin >> num3;
num2 += num3;
cout << num2;
}
but it keeps saying "Unexpected identifiers: num2, num3".
what should i change.
Re: Help with building a code....
In C/C++ entry point main() must exist. You never place your code just in global scope, except types declarations and global definitions.
Code:
#include <iostream>
using namespace std;
int main()
{
int num, num2, num3;
cin >> num;
num2 = 0;
for(int i = 0; i < num; i++)
{
cin >> num3;
num2 += num3;
cout << num2;
}
return 0;
}
Re: Help with building a code....
thanks for the input but this isn't done on visual studio, the program is compile using myprogramminglab.com where we dont have to include the header files and main int()
Re: Help with building a code....
Quote:
Originally Posted by
mejiac3
thanks for the input but this isn't done on visual studio, the program is compile using myprogramminglab.com where we dont have to include the header files and main int()
Given that it doesn't complain about 'num', only 'num2' and 'num3', maybe it only allows one variable declaration on a line?
Re: Help with building a code....
Quote:
Originally Posted by
mejiac3
thanks for the input but this isn't done on visual studio, the program is compile using myprogramminglab.com where we dont have to include the header files and main int()
If so, your issue has nothing to do with C++, or Visual C++ (which is supposed by forum title). The code I based strictly on yours one compiles in VC++ just fine, and works okay giving a correct result. You need to ask at myprogramminglab.com what's wrong with it.
Re: Help with building a code....
Quote:
Originally Posted by
mejiac3
thanks for the input but this isn't done on visual studio, the program is compile using myprogramminglab.com where we dont have to include the header files and main int()
The code that was posted is valid C++ that must compile successfully on any ANSI standard C++ compiler.
It doesn't matter if it's Visual Studio, gcc, Comeau, myprogramminglab.com, or JoeSchmoe's ANSI compiler, it must compile successfully. If it doesn't compile successfully, then your compiler is not an ANSI standard compiler and you're not learning C++ properly (you must know what headers to include when writing a C++ program, otherwise you are not learning the language properly).
So whatever this "myprogramminglab.com" is, it isn't a real compiler, and it isn't teaching you C++ in the way you must use it in a real environment.
Regards,
Paul McKenzie
Help with building a code....
Can someone help me to build a C++ program that can read data from a serial port (for eZ430 chronos watch)?
I have this school project and I don't have to much experience in using VB C++.
This are the datas that I have to use to communicate with the watch :
http://e2e.ti.com/support/microcontr...6/t/32714.aspx
Thanx!
P.S. Sorry for my bad english.
Re: Help with building a code....
I know this is 6 years late, but a search for the same problem brought me here.
For anyone needing help on this assignment in the future, myprogramminglab accepted the following answer:
Code:
int header = 0;
int sum = 0;
int input;
cin >> header;
for (int i = 0; i < header; i++){
cin >> input;
sum += input;
}
cout << sum;
Cheers.
Re: Help with building a code....
[When posting code, please use code tags so that the code is readable and formatted. Go Advanced, select the formatted code and click '#'.]