Now What?

This is a discussion on Now What? within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm very new at programming and I'm trying to get this to work. I want to make a program ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    9

    Now What?

    Hi, I'm very new at programming and I'm trying to get this to work. I want to make a program that figures out the average grade of tests for a marking period. The user inputs the number of tests and then for each one, he is prompted to input the grade and the program does the adding and dividing after the grades are input. I realize that this isn't practical at all. I am simply using it as a vehicle to improve my very limited skills. Here is what I have so far. . .

    Code:
    #include <iostream>
    int main ()
    {
    int a,b;
    cout<<"Enter number of grades: ";
    cin>>b;
    for (a=1;1<=a && a<=b;a++)
        {
        cout<<"Enter grade\n";
        }
        cin.ignore();
        cin.get();
        return 0;
    }
    As you can see, It isn't much but I consider it an accomplishment to get this far. I don't understand now, how to take those numbers and get the program to ask for a new grade and then to store and add them. An explanation of what to do next would be greatly appreciated.

  2. #2
    I'm less than sure.... abyssphobia's Avatar
    Join Date
    Aug 2004
    Posts
    112
    Well you have to put an array

    arra[10] // this is to store the grades For example 10 or whatever you want

    and when the user put the grades you can put for example

    cin>>n>>arr[a]; // to make it change , you can imagine that its like a file when you can store the elements you receive by the user.

    hope this help!!!!
    love,
    abyss
    Have I crossed the line?

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    To put what abyssphobia was trying to say in some clearer terms, you could create an array of data, which is simply a collection or sort of list of data, like so:
    Code:
    int grades[10];   //this will make an array that can hold 10 integers
    to get to a specific item in the array, you index it like this
    Code:
    grades[0] = 1;    //the item list starts at 0, not 1...remember that when creating your loop
    grades[1] = 2;    //this is the second item
    grades[n] = 10;  //this would be the nth item, make sure n is not less than 0
                               //or greater than the size of the array
    from that information, I'm sure you can write out your loop just fine.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    An even easier way to do this requires no arrays. Here is some pseudo-code:
    1. Set i = 0, sum = 0
    2. Get number of entries from the user, n
    3. Get the grade, t
    4. Set sum = sum + t;
    5. Set i = i + 1
    6. Perform steps 3-5 n times (a loop)
    7. Set avg = sum / i (casting appropriately, of course to maintain desired precision)

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>or greater than the size of the array
    Or equal to, also. For an array of size n, valid indices are in the range 0..(n-1).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    9
    Thanks everyone for your suggestions. I ended up using Zach's idea because it was simple and I haven't learned arrays yet. This is what I came up with. . .Does it look ok? I want this to be good because I'm learning by implementing whatever I learn into this program. . .

    Code:
    #include <iostream>
    int main ()
    {
    int a,n,t,sum;
    float avg;
    sum=0;
    cout<<"Enter number of grades: ";
    cin>>n;
    if (n==0)
    cout<<"Cannot divide by zero";
    else
    {
    for (a=1;1<=a && a<=n;a++)
        {
        cout<<"Enter grade ";
        cin>>t;
        sum=sum+t;
        }
        avg=sum/n;
        cout<<"Your average is : "<<avg;
    }
        cout<<"\nPress Enter to exit. . .";
        cin.ignore();
        cin.get();
        return 0;
    
    }
    Oh yes. One more quick question. . .What exactly does cin.ignore do? I saw it used in a program and it solved the problem of the program exiting without waiting for me to press enter. I just don't know why.

  7. #7
    Some Guy
    Join Date
    Jul 2004
    Posts
    32
    I read this after googling.
    The ignore() function is used with input streams. It reads and throws away characters until num characters have been read (defaults to 1) or until the character delim is read (which defaults to EOF).
    You can name your variables a little bit better to represent the data they actually contain.
    Lastly, you can initialize the variable 'a' inside the for loop, like so.
    Code:
    for (int a = 1; a<n; a++)
    Also, people generally use i, j, n for the names of variables in for loops.

    Hope that helps.

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Looks good. A couple little things I noticed.
    1. You include <iostream> (which is good!), but you never declare that you are using the std namespace. ('using namespace std;' after the headers is the easiest way - there have been numerous threads on this topic in case you're interested.) Your compiler really should have caught this.
    2. Both sum and n are integers, so the division yields integer division. To correct this, cast one or both to floats. ('avg = static_cast<float>(sum)/n;' is the most proper cast in C++. 'avg = float(sum) / n' would also work.)
    3. If you want, 'sum = sum + t' can be abbreviated 'sum += t'.

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