Thread: New to forums and need help

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    6

    New to forums and need help

    Hi, the name's Keyaki and, like the title says, I'm new here. And I need help with a tiny problem. And by "tiny", I mean its an issue that I should know how to solve myself but unfortunately, I'm at a loss.

    I scoured the forums for answers but I couldn't find anything, 'tis why I'm posting here.

    Here's the issue. I'm writing a simple program that takes a person's name and 5 recent tests grades and averages them using a module and a function and if-else-if statements.

    The problem is the IDE I'm using, Visual Studio '10, is giving me an error message under the '<' operators in the if-else-if statements in the Grade module.

    The error messages are simply state: "Expected an expression".

    I've checked the rest of the code and everything checks out and I've done if-else-if statements in code before and I don't recall encountering any error like this.

    Can anyone tell me, exactly what I'm doing wrong? Thanks.
    Code:
    #include<iostream>
    #include <string>
    
    using namespace std;
    
    double calcAverage(double grade1, double grade2, double grade3, double grade4, double grade5);
    void Grade(double average);
    
    int main()
    {
        char grade;
        char confirm;
        string firstName;
        string lastName;
        string name;
        
    
        double grade1;
        double grade2;
        double grade3;
        double grade4;
        double grade5;
    
        double average;
    
        cout << "What's your name?" << endl;
        getline(cin,name);
        
        cout << name << " ? Is that your name? Yes(y) or No(n)" << endl;
        cin >> confirm;
        
        
    
        while(confirm == 'n')
        {
            cout << "Then what is your first and last name?" << endl;
            cin >> firstName;
            cin >> lastName;
    
            cout << firstName <<" "<< lastName << ", Is that your name? Yes(Y) or No(N)" << endl;
            cin >> confirm;
    
        }
        
            cout << "Enter your 5 most recent test grades." << endl;
    
            cin >> grade1;
            cin >> grade2;
            cin >> grade3;
            cin >> grade4;
            cin >> grade5;
        
            average= calcAverage(grade1, grade2, grade3, grade4, grade5);
            Grade(average);
    
        cout << "Press any button to close. " << endl;
        cin.ignore().get();
        return 0;
    }
    
    double calcAverage(double grade1, double grade2, double grade3, double grade4, double grade5)
    {
        double averageResult;
    
        averageResult=grade1+grade2+grade3+grade4+grade5/5;
    
        return averageResult;
    }
    
    void Grade(double average)
    {
        if(average >= 100)
        {
            cout << "A+!" << endl;
        }
    
        else if(average >= 90 && < 100)
        {
            cout << "A!" << endl;
        }
    
        else if(average >= 80 && < 90)
        {
            cout << "B!" << endl;
        }
    
        else if(average >= 70 && < 80)
        {
            cout << "C!" << endl;
        }
    
        else if(average >= 60 && < 70)
        {
            cout << "D!" << endl;
        }
    
        else
        {
            cout << " F..." << endl;
        }
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>else if(average >= 90 && < 100)
    Should be:
    else if(average >= 90 && average < 100)
    Similarly for others.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    6
    Quote Originally Posted by Elysia View Post
    >>else if(average >= 90 && < 100)
    Should be:
    else if(average >= 90 && average < 100)
    Similarly for others.
    Wow. Like I said, so simple and yet I missed it.

    Now, I got another small problem.

    I changed the code around in the while loop. Instead of two cin statements, I replaced them with the getline method. But when I try to execute the while loop, it doesn't let me re-enter an input.

    Code:
    cout << "What's your name?" << endl;
        getline(cin,name);
        
        cout << name << " ? Is that your name? Yes(y) or No(n)" << endl;
        cin >> confirm;
        
        
    
        while(confirm == 'n')
        {
            cout << "Then what is your first and last name?" << endl;
       
    
            getline(cin,name);
    
            cout << name << ", Is that your name? Yes(Y) or No(N)" << endl;
            cin >> confirm;
    
        }
    Last edited by Keyaki; 12-31-2014 at 08:31 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is a pretty known problem. It has to do with the input buffer.

    getline first checks the input buffer to see if there's anything there, and there isn't, so it tries to read from your keyboard.
    Say you enter your name, e.g. Keyaki.

    Input buffer: Keyaki\n
    getline reads all of it so input buffer becomes empty (discards the \n).

    Now you enter y\n.
    cin >> confirm reads 'y', and leaves \n in the input buffer.

    The loop loops and getline sees "\n" in the input buffer, so it reads "" and discards the "\n".

    Solution is to either only use getline (compared to "\n") or use cin.ignore() after reading with cin >> confirm to clear the input buffer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    6
    Quote Originally Posted by Elysia View Post
    This is a pretty known problem. It has to do with the input buffer.

    getline first checks the input buffer to see if there's anything there, and there isn't, so it tries to read from your keyboard.
    Say you enter your name, e.g. Keyaki.

    Input buffer: Keyaki\n
    getline reads all of it so input buffer becomes empty (discards the \n).

    Now you enter y\n.
    cin >> confirm reads 'y', and leaves \n in the input buffer.

    The loop loops and getline sees "\n" in the input buffer, so it reads "" and discards the "\n".

    Solution is to either only use getline (compared to "\n") or use cin.ignore() after reading with cin >> confirm to clear the input buffer.
    Buffers. I remember working with those while working with Java.

    Thanks for the help

  6. #6
    Registered User
    Join Date
    Dec 2014
    Posts
    143
    I remember when I first started programming in c++ (which wasn't long ago) I had this problem. I ended up using the cin.ignore() method as Elysia previously stated.

  7. #7
    Registered User
    Join Date
    Dec 2014
    Posts
    6
    Quote Originally Posted by cmajor28 View Post
    I remember when I first started programming in c++ (which wasn't long ago) I had this problem. I ended up using the cin.ignore() method as Elysia previously stated.
    That's another thing. I've been doing C++ for about 3-4 years now and I've always used the system("pause") method. It wasn't until last year(considering that it is 2015, now) that I've been told that the cin.ignore().get() method is better but I've never understood why.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The problems with system("pause") aren't a good tradeoff for what it does.

    system() is one way, a poor way at that, of running a program within a program. Pause is a program that windows users have. You could even try it the next time you run a command window. It obviously just doesn't work in other places besides windows because "pause" doesn't exist or it might do something else. Who knows?

    And that's basically the problem with it. Using system to pause the program makes an assumption - pause exists, and it does what you want - when there are ways to do the same thing with standardized code. If you even need to do that thing. You will do other things in your programming life where this is just not a problem. Programs with a graphic interface work on event loops until the program is explicitly closed. Many command line programs do not need to be paused because either they're run in a separate window that won't automatically close, (this is the majority of times) allowing you to see the output, or they also run on a similar event loop and close when you want.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++/MFC Forums
    By mrafcho001 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-20-2005, 07:26 AM
  2. Any 3d forums around?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 08-09-2002, 11:45 AM
  3. Other Forums?
    By Fool in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 12-05-2001, 05:19 PM