Thread: Please help with this code, I am confused

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    Please help with this code, I am confused

    These are the instructions for the program:
    Write a flowchart and a program that will accept 5 letter grades as input from a user and convert each letter grade into grade points. Total all the grade points. Output the total grade points and the grade point average.
    NOTE: Use a nested if-then-else structure to convert the letter grade input into its numeric equivalent.

    Is this correct so far?:
    Code:
    char grade1;char grade2;char grade3;char grade4;char grade5; //Variables
    Code:
    cout <<
    "Enter Letter Grade #1: " << endl;cin >> grade1;cout << "Enter Letter Grade #2: " << endl;cin >> grade2;cout << "Enter Letter Grade #3: " << endl;cin >> grade3;cout << "Enter Letter Grade #4: " << endl;cin >> grade4;cout << "Enter Letter Grade #5: " << endl;cin >> grade5;if(grade1=A)
    {grade1=4.0;
    grade1= grade1+0;}
    else
    if(grade1=B)
    {grade1=3.0
    grade1=grade1+0
    }
    else
    {
    if(grade1=C)
    {grade1=2.0
    grade2=grade1+0
    }}
    else
    {
    if
    (grade1=D)
    {
    grade1=1.0
    grade1=grade1+0
    }
    }
    if(grade1==F){
    grade1=0
    grade1=grade1+0
    if
    (grade2=A)
    {
    grade1=4.0
    grade1=
    grade1+0
    }
    Else
    if(grade2=B)
    {
    grade1=3.0
    grade1=grade1+0
    }
    else
    If(grade2=C)
    {
    grade1=2.0
    grade2=grade1+0
    }
    else
    if
    (grade1=D)
    {
    grade1=1.0
    grade1=grade1+0
    }
    if(grade1=F){grade1=0grade1=grade1+0

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Please use regular code tags and font when posting your code and question.

    First thing that jumps out at me is that you're trying to use = to compare. That will not work. You must use == to compare values, as = assigns the value on the right side to the variable on the left side.

    Also, single characters are always in single quotes.

    Code:
    char myCharVariable = 'A';

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    That's really hard to read. I took a shot at reformatting it:

    Code:
    cout << 
    	"Enter Letter Grade #1: " << endl;cin >> grade1;
    cout << "Enter Letter Grade #2: " << endl;cin >> grade2;
    cout << "Enter Letter Grade #3: " << endl;cin >> grade3;
    cout << "Enter Letter Grade #4: " << endl;cin >> grade4;
    cout << "Enter Letter Grade #5: " << endl;cin >> grade5;
    
    
    if(grade1=A)
    {
    	grade1=4.0;
    	grade1= grade1+0;
    }
    else if(grade1=B)
    {
    	grade1=3.0
    	grade1=grade1+0
    }
    else
    {
    	if(grade1=C)
    		{
    			grade1=2.0
    			grade2=grade1+0
    		}
    }
    else
    {
    	if
    		(grade1=D)
    	{
    		grade1=1.0
    		grade1=grade1+0
    	}
    }
    if(grade1==F)
    {
    		grade1=0
    		grade1=grade1+0
    		if (grade2=A)
    		{
    			grade1=4.0
    			grade1= 
    			grade1+0
    		}
    		else if(grade2=B)
    		{
    			grade1=3.0
    			grade1=grade1+0
    		}
    		else if(grade2=C)
    		{
    			grade1=2.0
    			grade2=grade1+0
    		}
    		else if (grade1=D)
    		{
    			grade1=1.0
    			grade1=grade1+0
    		}
    		if(grade1=F)
    		{grade1=0grade1=grade1+0
    You can use "char" for the grade letters (char is "character") but not for floating point numbers. For this you can use floats:
    Code:
    char grade1;char grade2;char grade3;char grade4;char grade5; //letter variables
    
    float grade1num, grade2num, grade3num, grade4num, grade5num; //decimal variables
    What are all the "grade1=grade1+0" lines for? They don't do anything?

    Then use your "if" statements to set up these float variables for each grade. Think of it in words: how do you figure out what number to give to a letter grade?

    Code:
    If it an A
       set num to 5.0
    else if it is a B
       set num to 4.0
    else if it is a C
       set num to 3.0
    .... etc, for each of the 5 grades
    Towards the end it looks like you're trying to start summing the grades, or work out the average or something. I would recommend getting all the letters read in and converted to numbers before doing that. It'll be really simple then, just like:

    Code:
    float gradesum = grade1num + grade2num + grade3num + grade4num + grade5num;

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Code:
    char
    Code:
     grade1;char grade2;char grade3;char grade4;char grade5; 

    Code:
    float grade1num;float grade2num;float grade3num;float grade4num;float grade5num;float gradesum; 
    cout <<
    "Enter Letter Grade #1: " << endl;cin >> grade1;cout << "Enter Letter Grade #2: " << endl;cin >> grade2;cout << "Enter Letter Grade #3: " << endl;cin >> grade3;cout << "Enter Letter Grade #4: " << endl;cin >> grade4;cout << "Enter Letter Grade #5: " << endl;cin >> grade5; if (grade1 == 'A'){grade1num = 4.0;}else {if (grade2 == 'B') {grade2num = 3.0;}else {if (grade3 == 'C'){grade3num = 2.0;}else {if (grade4 == 'D'){grade4num = 2.0;}else {if (grade5 == 'F'){grade5num = 0;}else {cout << "Invalid grade" << endl;}}}}}gradesum = grade1num + grade2num + grade3num + grade4num + grade5num;cout << "The sum of the GPA's is: "<< gradesum << endl;cout << "The total GPA is :" << gradesum/5;   
    return
    0;}

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would suggest you learn to use arrays and properly write the code in a non-horrible way.
    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.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Try to compile it, and read the error messages you get. Keep fixing it until there are no warning or error left.

    Also, since you want others to look at your code, you should follow regular coding style conventions, like one ; per line.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Code:
    |    ^       |          ^
    |    |       |          |
    v    |       v          |
    what feel    like       
    kind you     formatting possible?
    of   that    code       way
    game playing in         annoying
    are  you     the        most
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with following code
    By SeriousTyro in forum C Programming
    Replies: 1
    Last Post: 08-25-2009, 10:37 AM
  2. Which part/code of WinMain() calls WndProc()? .. Confused!
    By csonx_p in forum Windows Programming
    Replies: 5
    Last Post: 04-08-2008, 02:47 AM
  3. V confused
    By spudtheimpaler in forum C Programming
    Replies: 2
    Last Post: 03-05-2004, 09:14 AM
  4. I'm confused as to what exactly is going on in this code
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-11-2002, 02:22 PM
  5. I am confused with this code that somebody gave me
    By face_master in forum C++ Programming
    Replies: 14
    Last Post: 11-16-2001, 01:43 AM