Need Help

This is a discussion on Need Help within the C++ Programming forums, part of the General Programming Boards category; I have been given an assignment to write a C++ program that inputs integer numbers bewtween 0 and 100 until ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    18

    Need Help

    I have been given an assignment to write a C++ program that inputs integer numbers bewtween 0 and 100 until -1 is entered and then outputs the average of those numbers. The average must be shown with two decimal places, even if they are both 0. Your program must use at least one function. Please remember our class is still in the basic functions of programming. I also had to input those two lines with cout.setf, this was require by the teacher. Thanks for any help, our teacher is one of the worst ones I have had in my entire time at college.



    [CODE]

    #include<iostream.h>
    using namespace std;

    void prompt();

    main()
    {
    cout.setf(ios::fixed, ios::floatfield);
    cout.setf(ios::showpoint);

    int score=0;
    int total=0;
    int count=0;
    int sentinel=-1;
    while (score!=sentinel)
    {
    prompt();
    cin >> score;
    if (score!=sentinel)
    {
    total=total+score;
    count=count+1;
    }
    if (count > 0)
    cout << "The average is: "
    << total/count << endl;
    else
    cout << "No genuine scores were entered"<< endl;
    return 0;
    }
    void prompt ()
    {
    cout << "Enter a number: ";
    }

  2. #2
    Registered User
    Join Date
    Oct 2003
    Posts
    28
    Code:
    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    void prompt();
    
    main()
    {
      cout.setf(ios::fixed, ios::floatfield);
      cout.setf(ios::showpoint);
      cout.precision(2);
    
      int score=0;
      int total=0;
      int count=0;
      int sentinel=-1;
      while (score!=sentinel)
        {
          prompt();
          cin >> score;
          if (score!=sentinel)
    	{
    	  total=total+score;
    	  count=count+1;
    	}
        }
      if (count > 0)
        cout << "The average is: "
    	 << (double)total/(double)count << endl;
      else
        cout << "No genuine scores were entered"<< endl;
      return 0;
    }
    void prompt ()
    {
      cout << "Enter a number: ";
    }

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    66
    total=total+score;

    has capacity to only have a total of 2^32.. 4 billion. If you enter 2 numbres that amount to more than that the result will be quite unexpected.

    I would declare:

    double total = 0.0;

    and at least avoid the obvious overflow...

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    achacha, he said that the inputted numbers would be less than or equal to 100. You'd be there for quite some time entering 100 to get to the max integer. I don't think declaraing total a double is necessary. I do think, however, that it could be made an unsigned long.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    18

    Thanks for the help guys.

    I appreciate your input, I got stuck with this computer programming class. I did not realize this computer class was for computer programming majors. I am a finance major. As long as the program works and the teacher gives me a B or C grade I am happy. It does not help that this is one of the worst teachers have had. I'll be posting more questions in a couple of weeks.
    Thanks.

    Shawn

    Also, what is parse error.

  6. #6
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    A parse error is an error that occurs during the parsing phase of the compile process, it is also called the syntactic analysis.
    Last edited by lyx; 10-04-2003 at 01:45 AM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21