#include <iostream>
using namespace std;

int main()
{
    int c_hrs1 , c_hrs2 , c_hrs3 , c_hrs4;
    float gpa_1 , gpa_2 , gpa_3 , gpa_4;

    int total_c_hrs= c_hrs1+c_hrs2+c_hrs3+c_hrs4;
    float cgpa= ((c_hrs1*gpa_1)+(c_hrs2*gpa_2)+(c_hrs3*gpa_3)+(c_hrs4*gpa_4))/total_c_hrs;
        
    cout<<"**********************************************************************"<<endl;
    cout<<"\t Welcome to GPA Calculator for Dummies"<<endl;
    
    cout<<"Please enter you GPA in course 1: ";
    cin>>gpa_1;
    cout<<"Please enter the credit hours for course 1:";
    cin>>c_hrs1;
    
    cout<<"Please enter you GPA in course 2: ";
    cin>>gpa_2;
    cout<<"Please enter the credit hours for course 2:";
    cin>>c_hrs2;
    
    cout<<"Please enter you GPA in course 3: ";
    cin>>gpa_3;
    cout<<"Please enter the credit hours for course 3:";
    cin>>c_hrs3;
    
    cout<<"Please enter you GPA in course 4: ";
    cin>>gpa_4;
    cout<<"Please enter the credit hours for course 4:";
    cin>>c_hrs1;
    
    cout<<"**********************************************************************"<<endl;
     
    
[B]    cout<<"Your GPA for this semester is:"<<cgpa;      
    
    cin.get();[/B]
    return 0;
}

It doesnt even display the last cout>>

Recommended Answers

All 5 Replies

Your cin.get doesn't work because it read the newline character.

use another cin.get().

And if you're interested why that is required, read this

int c_hrs1 , c_hrs2 , c_hrs3 , c_hrs4;
    float gpa_1 , gpa_2 , gpa_3 , gpa_4;

    int total_c_hrs= c_hrs1+c_hrs2+c_hrs3+c_hrs4;
    float cgpa= ((c_hrs1*gpa_1)+(c_hrs2*gpa_2)+(c_hrs3*gpa_3)+(c_hrs4*gpa_4))/total_c_hrs;

This will not work because all of those variables used in initializing total_c_hrs and cgpa have not been initialized. You should move the second two lines to after all of the input.

cout<<"Please enter you GPA in course 4: ";
    cin>>gpa_4;
    cout<<"Please enter the credit hours for course 4:";
    cin>>c_hrs1;

Copy/paste coding can be dangerous. ;) You forgot to change c_hrs1 to c_hrs4, which leaves c_hrs4 uninitialized in the calculation of total_c_hrs and cgpa.

It doesnt even display the last cout>>

If you are using cin.get() to keep the console window open then the last cout would happen too fast for you to see. But a terminating line break is a good idea anyway.

operator<< trims leading white space and stops reading on white space by default, so there will be a '\n' character still sitting in the stream for cin.get() to read, like firstPerson said. But another cin.get() is just a hack to fix this specific problem. If there is more than one character at the end of the stream, the same thing will happen. You can fix the problem generically like this:

cin.ignore(1024, '\n');

This reads and discards up to 1024 characters or until '\n' is found. It should clear the stream out so that the next cin.get() will block. 1024 is an arbitrary number though, numeric_limits<streamsize>::max() is a better choice because streamsize is the size of the stream buffer, so the buffer will not have more characters than the largest value streamsize can hold. You can find numeric_limits in the <limits> header, and streamsize in the <ios> header:

#include <iostream>
#include <limits>
#include <ios>
using namespace std;

int main()
{
    int c_hrs1 , c_hrs2 , c_hrs3 , c_hrs4;
    float gpa_1 , gpa_2 , gpa_3 , gpa_4;

    cout<<"**********************************************************************"<<endl;
    cout<<"\t Welcome to GPA Calculator for Dummies"<<endl;

    cout<<"Please enter you GPA in course 1: ";
    cin>>gpa_1;
    cout<<"Please enter the credit hours for course 1: ";
    cin>>c_hrs1;

    cout<<"Please enter you GPA in course 2: ";
    cin>>gpa_2;
    cout<<"Please enter the credit hours for course 2: ";
    cin>>c_hrs2;

    cout<<"Please enter you GPA in course 3: ";
    cin>>gpa_3;
    cout<<"Please enter the credit hours for course 3: ";
    cin>>c_hrs3;

    cout<<"Please enter you GPA in course 4: ";
    cin>>gpa_4;
    cout<<"Please enter the credit hours for course 4: ";
    cin>>c_hrs4;

    int total_c_hrs= c_hrs1+c_hrs2+c_hrs3+c_hrs4;
    float cgpa= ((c_hrs1*gpa_1)+(c_hrs2*gpa_2)+(c_hrs3*gpa_3)+(c_hrs4*gpa_4))/total_c_hrs;

    cout<<"**********************************************************************"<<endl;
    cout<<"Your GPA for this semester is:"<<cgpa<<endl;    

    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
    return 0;
}
commented: Many fine points +36

oh god im so lame!

When you have a Integral or compound data type variable
and you ask the user to enter a number like so :

int num = 0;
cin >> num;

The cin object does not read in spaces or newline. In this case
when you input say 5<enter> then press enter, the 5 will be
read into the variable num, and the part when you press enter,
the computer sees this as a new line character and leaves it in
the input stream. So the next time when you read a character,
it reads that newline. Thats whats your cin.get() is doing.
Its reading the newline character thats stuck in the input stream.
you can use another cin.get(), or clear the stream, or
read the integers as strings and the convert it accordingly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.