Thread: Depending on length of name, output changes?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Depending on length of name, output changes?

    I cannot keep the << firstName << and << lastName << from moving "Grade Points" in the output. I am instructed to keep these in the same line but If I do it varies depending on the name.

    Code:
    // Output Area of Program
    		cout << endl;
    		cout << endl;
    		cout << "                  Class Heading" << endl;
    		cout << "              Date of Report:04/25/2005" << endl;
    		cout << endl;
    		cout << endl;
    		cout << "Student: "<< lastName <<", "<< firstName  <<      "Grade  Points " << endl;
    		cout << endl;
    		cout << endl;
    		cout << "         Exam 1 grade, points:" <<"               "<<exam1<<" "<< endl;
    		cout << "         Exam 2 grade, points:" <<"               "<<exam2<<" "<< endl;
    		cout << "         Exam 3 grade, points:" <<"               "<<exam3<<" "<< endl;
    		cout << "     Final Exam grade, points:" <<"               "<<finalExam<<" "<<endl;
    		cout << "          Lab 1 grade, points:" <<"               "<<lab1<<" "<<endl;
    		cout << "          Lab 2 grade, points:" <<"               "<<lab2<<" "<<endl;
    		cout << "          Lab 3 grade, points:" <<"               "<<lab3<<" "<<endl;
    		cout << "          Lab 4 grade, points:" <<"               "<<lab4<<" "<<endl;
    		cout << endl;
    		cout << "Total Daily Points = " << dayPnts << endl;
    		cout << endl;
    		cout << "Semester Points    = " << semPoints << endl;
    		cout << endl;
    
    OUTPUT
    /*
    
                      Class Heading
                  Date of Report:04/25/2005
    
    
    Student: xxxxx, xxxxxxxxGrade  Points
    
    
             Exam 1 grade, points:               90
             Exam 2 grade, points:               90
             Exam 3 grade, points:               90
         Final Exam grade, points:               90
              Lab 1 grade, points:               90
              Lab 2 grade, points:               90
              Lab 3 grade, points:               90
              Lab 4 grade, points:               90
    
    Total Daily Points = 15
    
    Semester Points    = 91.5
    
    xxxxxxxx xxxxx is an outstanding student.
    */
    
    /*
    
    
                      Class Heading
                  Date of Report:04/25/2005
    
    
    Student: xx, xxGrade  Points
    
    
             Exam 1 grade, points:               90
             Exam 2 grade, points:               90
             Exam 3 grade, points:               90
         Final Exam grade, points:               90
              Lab 1 grade, points:               90
              Lab 2 grade, points:               90
              Lab 3 grade, points:               90
              Lab 4 grade, points:               90
    
    Total Daily Points = 90
    
    Semester Points    = 166.5
    
    xx xx is an outstanding student.
    
    Enter NONE to end Loop!
    Enter last name: NONE
    Press any key to continue
    */[/COLOR]

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You need to use some formatting commands like setw perhaps.

    Code:
    #include <iomanip>
    using namespace std;
    
    ...
    
    cout << "Student: " << set(20) << lastName << ", " << setw(20) << firstName  << " Grade  Points " << endl;
    Choose something appropriate for the two values based on the number of characters in your lastName and firstName variables.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    I don't think there really is any kind of standard way of keeping "Grade Points" in one location, regardless of how long the string before it is. Instead, why don't you just enter a new line with "Grade Points" only on it? Also, it might help you to know the escape character for entering a tab, '\t'.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    hk_mp5pdw, I tried what you ask and even substituted my line for yours and even included the directive #include <iomanip> but after I compiled it said undelared identifier, What did I do wrong?

    Krak, I want to put it on another line but unfortunately my Instructor insists that it must stay where it belongs. My instructor said that I have to move the insertion or move the " " to get the desired result the he or she asked for. I'm not familiar with '\t' is there a tutorial here that explains this in detail?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Hp7130p
    hk_mp5pdw, I tried what you ask and even substituted my line for yours and even included the directive #include <iomanip> but after I compiled it said undelared identifier, What did I do wrong?
    Did you forget the using namespace std; part? What other headers are you including? What compiler are you using? What is the exact error message and the line of code that produces that error message?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    ...
    << set(20) << lastName << ", " << setw(20)
    ....

    There was a typo error after I fixed it in my program it ran ok but I am still having problems with the names moving the "grade" and "points" heading . Even though I change the numeric value the same problem occurs.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Maybe you want to use the left justified flag:
    Code:
    cout << left << "Student: " << set(20) << lastName << ", " << setw(20) << firstName  << " Grade  Points " << endl;

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    swoopy, I used the "left justified flag and It work great! Is there any more Info or tutorials on this? Just want to know because I'm that kind of guy. Thanks a million

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Is there any more Info or tutorials on this?
    A good reference for the C++ standard library is at www.cppreference.com

    And here at www.cprogramming.com there are some good tutorials and FAQ

    Maybe someone knows of a good I/O tutorial.
    Last edited by swoopy; 04-21-2005 at 07:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  2. 2d game
    By JordanCason in forum Game Programming
    Replies: 5
    Last Post: 12-08-2007, 10:08 PM
  3. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  4. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM