GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Feb-2009, 23:32
tpetsovi tpetsovi is offline
New Member
 
Join Date: Feb 2009
Posts: 7
tpetsovi has a little shameless behaviour in the past
Question

Can someone please help me understand why this won't work right?


I don't quite understand why this won't work. Also we are not using arrays or functions other than main. were just into the basics and starting to cover things in <iomanip> and such. Thanks in advance.

CPP / C++ / C Code:
#include <iostream>
using namespace std;

void main ()
{
	const double A = 4.0;
	const double B = 3.0;
	const double C = 2.0;
	const double D = 1.0;
	const double F = 0.0;
	double LetterGrade_1 = 0, LetterGrade_2 = 0, CreditHours_1 = 0; 
	double TotalPoints = 0, TotalCreditHours = 0, CreditHours_2 = 0, GPA = 0;

	cout << "Please enter letter grade of first class --> ";
	cin >> LetterGrade_1;
	cout << endl;

	cout << "Please enter number of credit hours for first class --> ";
	cin >> CreditHours_1;
	cout << endl;

	cout << "Please enter letter grade for second class --> ";
	cin >> LetterGrade_2;
	cout << endl;

	cout << "Please enter number of credit hours for second class --> ";
	cin >> CreditHours_2;
	cout << endl << endl;

	TotalPoints = (LetterGrade_1 * CreditHours_1) + (LetterGrade_2 * CreditHours_2);
	TotalCreditHours = CreditHours_1 + CreditHours_2;
	GPA = TotalPoints / TotalCreditHours;

	if (GPA < 2.0)
	{
		cout << "You are doing poorly." << endl << endl;
	}

	else if (GPA >= 3.5)
	{
		cout << "Congratulations, doing good." << endl << endl;
	}
}
Last edited by admin : 15-Feb-2009 at 23:41. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 15-Feb-2009, 23:58
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Can someone please help me understand why this won't work right?


Quote:
Originally Posted by tpetsovi
I don't quite understand why this won't work....


1. What input did you give to the program?
2. What did you expect to see from the program?
3. What output did the program give you?
4. What do you not understand about the difference betwee, (2) and (3)?

Regards,

Dave
  #3  
Old 16-Feb-2009, 00:05
tpetsovi tpetsovi is offline
New Member
 
Join Date: Feb 2009
Posts: 7
tpetsovi has a little shameless behaviour in the past

Re: Can someone please help me understand why this won't work right?


Well I started by giving the first input a letter A and from there it displays every line of 'cout' and calculates nothing. The program should let me input 2 letter grades and 2 credit hour values and from there it should calculate the GPA, instead like I had said it only lets me input the first letter grade and after I press enter it displays every cout line of code and the program waits for me to end it. So I'm not sure why after I input one value and press enter why it runs through every line of code, displaying the ones it should but never let me input anything past the first and it calculates nothing.
  #4  
Old 16-Feb-2009, 00:10
tpetsovi tpetsovi is offline
New Member
 
Join Date: Feb 2009
Posts: 7
tpetsovi has a little shameless behaviour in the past

Re: Can someone please help me understand why this won't work right?


This is the output.

Please enter letter grade of first class --> A
Please enter number of credit hours for first class -->

Please enter letter grade of second class -->
Please enter number of credit hours for the second class -->

Press any key to continue...

Note, after the first value is inputed and the enter key is pressed, the program displays what you see above and leaves everything blank and waits for the program to be ended.
  #5  
Old 16-Feb-2009, 00:52
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Can someone please help me understand why this won't work right?


Quote:
Originally Posted by tpetsovi
Please enter letter grade of first class --> A
Please enter number of credit hours for first class -->

Huh???

You have this:
CPP / C++ / C Code:
    double LetterGrade_1 = 0, LetterGrade_2 = 0, CreditHours_1 = 0;
    double TotalPoints = 0, TotalCreditHours = 0, CreditHours_2 = 0, GPA =
        0;

Your so-called "letter grade" variables are double precision floating point variables, so you will have to give it numbers. If you enter a non-numeric quantity, the input stream (cin) enters a "Fail" state, and it can not be used for anything else until it is cleared.

The result is that further attempts to use "cin>>" will fail (that is, the "cin>>" will return to the program without reading anything from the user).

Also, for debugging purposes, I would probably print out values that were entered and other intermediate calculations.
Something like;
CPP / C++ / C Code:
    cout << "Please enter letter grade of first class --> ";
    cin >> LetterGrade_1;
    cout << "You entered " << LetterGrade_1 << endl;

    cout << "Please enter number of credit hours for first class --> ";
    cin >> CreditHours_1;
    cout << "You entered " << CreditHours_1 << endl;
    cout << endl;

    cout << "Please enter letter grade for second class --> ";
    cin >> LetterGrade_2;
    cout << "You entered " << LetterGrade_2 << endl;
    cout << endl;

    cout << "Please enter number of credit hours for second class --> ";
    cin >> CreditHours_2;
    cout << "You entered " << CreditHours_2 << endl;
    cout << endl << endl;

    TotalPoints = (LetterGrade_1*CreditHours_1) + (LetterGrade_2*CreditHours_2);
    cout << "Total Points = " << TotalPoints << endl;

    TotalCreditHours = CreditHours_1 + CreditHours_2;
    cout << "Total Credit Hours = " << TotalCreditHours << endl;

    GPA = TotalPoints / TotalCreditHours;
    cout << "GPA = " << GPA << endl;

    if (GPA < 2.0) {
        cout << "You are doing poorly." << endl << endl;
    }

    else if (GPA >= 3.5) {
        cout << "Congratulations, doing good." << endl << endl;
    }

A run might look like:
Code:
Please enter letter grade of first class --> 3.0 You entered 3 Please enter number of credit hours for first class --> 5 You entered 5 Please enter letter grade for second class --> 2.0 You entered 2 Please enter number of credit hours for second class --> 3 You entered 3 Total Points = 21 Total Credit Hours = 8 GPA = 2.625

Of course, after debugging, you might remove any output statements that are not part of the assignment. Also, I probably wouldn't ask the user to enter a "letter grade" if the program requires a number, so I might change the prompts so that the user would have a clue as to what is expected.

Regards,

Dave
  #6  
Old 16-Feb-2009, 01:08
tpetsovi tpetsovi is offline
New Member
 
Join Date: Feb 2009
Posts: 7
tpetsovi has a little shameless behaviour in the past

Re: Can someone please help me understand why this won't work right?


Thanks, I did figure out that I was using the floating point instead of a character data type.

My next question is, since I have to have the user input either A, B, C, D, F, and the ASCII value for those are 65, 66, 67, 68, and 70, do I need to do a bunch of 'if' 'else if' statements.

For example
CPP / C++ / C Code:

if (LetterGrade_1 == 'A')
{    
    TotalPoints = 4 * CreditHours_1;
    // More code in here as well, but I think you'll know what I mean.
   // also do the same for a B and so on but with different values.
}


Or can I somehow reasign a value to the character A to equal 4.

I hope that I'm not being to confusing, and I really appretiate your help, but understand I'm new to this, but I want to learn, I love programming and I want to learn this stuff the right way. Again thanks for the help.

Also I don't know how to use the debugger, if thats something you could explain to me that would be awesome. Thanks Dave
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Work at home. English proofreader needed. $14.50/hour cascade_soft Member Announcements, Advertisements & Offers 0 05-Jun-2006 01:29
Random access files Krazy_yA C Programming Language 6 07-May-2006 02:44
How do web redirection scripts work? rhino1616 Web Design Forum 9 27-Oct-2003 10:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:19.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.