i need help with a code (really easy)

This is a discussion on i need help with a code (really easy) within the C++ Programming forums, part of the General Programming Boards category; Im new with C++ . Its my third day using it. I know turbo pascal. I have a code with ...

  1. #1
    yorge
    Guest

    Question i need help with a code (really easy)

    Im new with C++ . Its my third day using it. I know turbo pascal. I have a code with an array that has something wrong and i dont know it. plz tell me wuts wrong. thx. dont laugh at my program . im just practising to learn the language.

    #include <iostream.h>

    int a;
    double b;
    char c, question;
    bool d;

    void favthings ()
    {
    d= 1;
    while (d == 1)
    {
    cout <<"Write your favorite letter"<<endl;
    cin >> c ;
    cout <<"Now write your favorite number"<<endl;
    cin >> a;
    cout <<"Now write your grade poing average"<<endl;
    cin >> b ;
    cout <<"Your favorite letter is "<< c <<" Your favorite number is "<< a <<" And your GPA is "<< b<< endl;;
    cout <<endl;
    cout <<"Do you want to input everything again?(y/n)"<<endl;
    cin >> question ;
    if (question == 'y')
    d= 1;
    if (question == 'n')
    d= 0;
    }
    }

    void gradearray ();
    {
    cout <<"Input your last 5 grades to get the average !!!"<<endl;
    int arraygrade[4] ;
    cin << arraygrade ;
    cout <<arraygrade ;
    }




    main ()
    {

    favthings ();
    gradearray ();
    cout <<endl;
    cout <<"BYE"<<endl;
    cout <<endl;
    cout <<"Made by l33t"<<endl;
    cout <<endl;
    return 0;
    }

  2. #2
    Unregistered
    Guest
    You must get each element in an array separately. You cannot use here
    cin >> array[4]
    instead use something like
    for(int count=0;count<4;count++)
    cin>>array[count]

  3. #3
    Unregistered
    Guest
    thx a lot.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    cout <<"Your favorite letter is "<< c <<" Your favorite number is "<< a <<" And your GPA is "<< b<< endl;;
    you dont need 2 ;;'s at the end, 1 is sufficient

    main ()
    {
    It should be int main ()

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    yorge,

    Your function code:

    void gradearray ();
    {
    cout <<"Input your last 5 grades to get the average !!!"<<endl;
    int arraygrade[4] ;
    cin << arraygrade ;
    cout <<arraygrade ;
    }


    Be careful with your insertion and extraction operators. (<< and >>)

    You've already seen the help with inserting into an array, but note that you prompt for five grades and only allocate enough space for four entries in your array.

    Occasionally, it's confusing to someone new to the language that in an array declaration, int array[x]; for instance, that the array indices run from 0 to x-1, not 0 to x.

    When you write your code to actually calculate the average, you will want read from your array with a FOR loop like the one used to input your data, obviously modifying the code to accumulate the grades and divide by the number of data entries, i.e. 'count'.

    Example:

    int gpa, temp = 0;

    for(int count=0;count<5;count++)
    {
    temp = temp + array[count];
    }

    gpa = temp / count;

    Note that 'temp' is set equal to 0 initially because you have no way of knowing what in the world it may contain otherwise. With 'gpa' this isn't really necessary since the right side of the assignment operator ( = ) will determine its value.

    Finally, (you were afraid there wasn't going to be a "Finally...", weren't you?), whether you show any decimal places in your answer will be dependent on how you "cast" your variables. If all are declared to be 'int', you'll lose any decimal places that may have been displayed if 'floats' had been used. That is, integer math will truncate the decimal portion of your answer leaving you with a whole number (no rounding will occur).







  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    It's not an error, but you don't need to put consecutive cout's like you have, and you don't need tons of endl's. It's a small extra bit of processing time.
    Code:
    cout << "\nBYE\n\n" << "Made by lt33" << endl;
    does the same thing your last lines do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM

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