Thread: Help with first c++ program (math-related)

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    [SOLVED]Help with first c++ program (math-related)

    Well, this is my first c++ program assignment.
    I was pretty proud of myself after I finished it but I'm having a small problem.

    This program is designed to output a few weighted grades from a set of 4 scores.

    The quizzes need to be weighted at 30% and the tests weighted at 70%

    It's set up, and I think it runs just fine, but my problem is, my weighted scores are not the same as the example's is

    The sample output is
    How many student you want to process: 1
    Enter next student’s name: Doug Talbert
    Enter quiz 1 quiz 2 test 1 and test 2 scores of Doug Talbert: 93 96 99 97
    Name: Doug Talbert
    Quiz: 27
    Test: 67
    Total: 94
    Grade: A

    Here is my output
    How many student you want to process: 1
    Enter next student’s name: Doug Talbert
    Enter quiz 1 quiz 2 test 1 and test 2 scores of Doug Talbert: 93 96 99 97
    Name: Doug Talbert
    Quiz: 28.35
    Test: 68.6
    Total: 96
    Grade: A
    http://img571.imageshack.us/img571/379/problemeu.jpg


    As you can see, I'm not getting exactly what I should.

    Also, I have one more problem, how do I get rid of those pesky decimals :P





    Code:
    
    
    /*****************************\
    *                             *
    *   9/30/2010                 *
    *   Programming Assignment 1  *
    \*****************************/
    
    #include <iostream>
    #include <cmath>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    int main(){
    
    	int totalquiz,totaltest,total,students,quiz1,quiz2,test1,test2;
    	char grade;
    	char name[21];
    
    	cout << "How many student you want to process: ";	
    	cin >> students;
    	cout << endl;
    
    	if (students >=1){
    
    		if (students<=50){	
    			
    			while (students){															 
    				cin.ignore();
    				cout << "Enter next student's name: ";
    				cin.getline (name,21);
    				cout << endl;
    
    				cout << "Enter quiz 1 quiz 2 test 1 and test 2 scores of " << name << " ";
    				cin >> quiz1 >> quiz2 >> test1 >> test2;
    
    				totalquiz = (quiz1+quiz2) / 2 * .3 ;
    				totaltest = (test1+test2) / 2 * .7 ;
    				total = totalquiz + totaltest;
    
    				if (total >= 90)
    					grade = 'A';
    				else if (total >= 80)
    					grade = 'B';
    				else if (total >= 70)
    					grade = 'C';
    				else if (total >= 60)
    					grade = 'D';
    				else if (total <  60)
    					grade = 'F';
    
    				cout << endl;
    				cout << setw(15) << "Name:" << setw(21) << name << endl;
    				cout << setw(15) << "Quiz:" << setw(21) << totalquiz << endl;
    				cout << setw(15) << "Test:" << setw(21) << totaltest << endl;
    				cout << setw(16) << "Total:" << setw(20) << total << endl;
    				cout << setw(16) << "Grade:" << setw(19) << grade << endl << endl;
    				students--;
    			}
    		}
    		else{
    			cout << "Number of students must be 50 or less, please try again\n";
    			return 0;}
    		}
    	else{ 
    		cout << "Number of students must be a number over 0, please try again\n";
    		return 0;}
    	}
    Last edited by necrovamp; 09-23-2010 at 12:39 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > PS> my code style is not complete, I was going to beautify it after I finish with comments ect.. :P
    More people might read it if it looks good to start with.

    Try doing the whole thing as integer maths, so you truncate at every possible step.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Well, It does look good in the program, but I didnt think people would want the entire program in the post, i guess I was wrong.

    As for the integer thing, unless I did something wrong it just removed the remainders


    Quote Originally Posted by Salem View Post
    > PS> my code style is not complete, I was going to beautify it after I finish with comments ect.. :P
    More people might read it if it looks good to start with.

    Try doing the whole thing as integer maths, so you truncate at every possible step.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    quiz1/2 + quiz2/2 != (quiz1+quiz2)/2
    when doing integer maths.

    > i guess I was wrong.
    It was wrong to assume that you could post any randomly formatted crud and expect people to read it.

    Even if it is only a snippet.

    But then you've edited the original, and I no longer care to debate the issue with you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    lol, the original was like that except for about 5 blank lines, I guess I should really think carefully before posting on this forum.

    anyways thanks, you have been very helpful, as I said this is my 1st program
    I should be entitled to a few mistakes

    I'll try to figure this out, I'll post again on this forum when I have finished this class on c++, maybe then I'll be able to organize my information to everyone's standards

    Quote Originally Posted by Salem View Post
    quiz1/2 + quiz2/2 != (quiz1+quiz2)/2
    when doing integer maths.

    > i guess I was wrong.
    It was wrong to assume that you could post any randomly formatted crud and expect people to read it.

    Even if it is only a snippet.

    But then you've edited the original, and I no longer care to debate the issue with you.
    Last edited by necrovamp; 09-23-2010 at 01:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. Math program help.
    By Deadlyaim in forum C++ Programming
    Replies: 2
    Last Post: 09-29-2006, 07:57 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread