-
March 10th, 2012, 12:13 AM
#1
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.
-
March 10th, 2012, 04:56 AM
#2
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;
}
Best regards,
Igor
-
March 10th, 2012, 04:04 PM
#3
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()
-
March 10th, 2012, 04:12 PM
#4
Re: Help with building a code....
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?
-
March 10th, 2012, 07:54 PM
#5
Re: Help with building a code....
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.
Best regards,
Igor
-
March 10th, 2012, 08:45 PM
#6
Re: Help with building a code....
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
-
March 11th, 2012, 04:05 AM
#7
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.
-
February 6th, 2018, 05:35 PM
#8
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.
Last edited by 2kaud; February 7th, 2018 at 05:15 AM.
Reason: Added code tags
-
February 7th, 2018, 05:19 AM
#9
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 '#'.]
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|