Help with building a code....
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Help with building a code....

  1. #1
    Join Date
    Feb 2012
    Posts
    8

    Question 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.

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,582

    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

  3. #3
    Join Date
    Feb 2012
    Posts
    8

    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()

  4. #4
    Join Date
    Jan 2009
    Posts
    596

    Re: Help with building a code....

    Quote Originally Posted by mejiac3 View Post
    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?

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,582

    Re: Help with building a code....

    Quote Originally Posted by mejiac3 View Post
    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

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help with building a code....

    Quote Originally Posted by mejiac3 View Post
    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

  7. #7
    Join Date
    Mar 2012
    Posts
    1

    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.

  8. #8
    Join Date
    Feb 2018
    Posts
    1

    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

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,043

    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++17 Compiler: Microsoft VS2017 (15.5.7)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)