Thread: First part of string runs but 2nd. wont

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    9

    First part of string runs but 2nd. wont

    Hi all i have an assignment to take enter 5 grades then average them, then enter 2 whole numbers and divide them to output one whole number with it's remainder.I'm stuck at the moment because it runs the 1st half of the program then the program ends never even starting the 2nd half of it here is what I have so far.

    Code:
    #include <iostream>
    #include <string>
    #include <int>
    using namespace std;
    int main()
    {
    
        
        
        
        
    
    //declaring variables
        
    string name;
    
    
    
    double grade_1,grade_2,grade_3,grade_4,grade_5;
    
    double avg;
    
    
    //get input from user
    cout << "Please enter your name:"  ";
    cin >> name;
    cout << endl;
    
    cout << "Enter first grade:"  ";
    cin >> grade_1 ;
    if(grade_1<0)
    grade_1=0;
    cout << endl;
    
        cout << "Enter second grade:"  ";
    cin >> grade_2 ;
    if(grade_2<0)
    grade_2=0;
    cout << endl;
    
    cout << "Enter third grade:"  ";
    cin >> grade_3 ;
    if(grade_3<0)
                grade_3=0;
    cout << endl;
    
    cout << "Enter forth grade:"  ";
    cin >> grade_4 ;
    if(grade_4<0)
                grade_4=0;
    cout << endl;
    
    cout << "Enter fifth grade:"  ";
    cin >> grade_5 ;
    if(grade_5<0)
                grade_5=0;
    cout << endl;
    
    
    
    // Finding the avrage
    avg = (grade_1+grade_2+grade_3+grade_4+grade_5) /5 ;
    cout << "Your average is: " << avg;
    
    
    int 1stNumber;
    
         double 2ndNumber;
    
    cout << "enter 2 whole numbers: " << endl; 
    cin >> 1stNumber >> 2ndNumber;
    cout <<1stNumber  << " divided by " << 2ndNumber
             << " is " << (1stNumber / 2ndNumber) << endl
             << "with a remainder of " << (1stNumber % 2ndNumber)
             cout << endl;
    return 0;
    }
    Any help or comments would be appreciated.
    Last edited by guyver074; 09-02-2012 at 07:10 AM. Reason: updated code.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You have two functions main!You must have only one!

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    ok thanks! I did take the 2nd main function out but still getting the same result.
    Last edited by guyver074; 09-02-2012 at 07:04 AM.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Also a good tactic is to check if the input is ok.For example grades must a non-negative number.Check if this happens and if not take a dicision
    e.g.
    Code:
    int main()
    {
    
         cout << "Enter third grade:"  ";
         cin >> grade_3 ;
         cout << endl;
         if(grade_3<0)//chech the input
                grade_3=0;//take a decision
           .....
         return 0;

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    oooh never though of that.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by guyver074 View Post
    ok thanks! I did take the 2nd main function out but still getting the same result.
    Now i saw your edit.Post the new code please.
    Also welcome to the forum!

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    Thanks std10093!!

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Please don't modify your posts to change the code, it disrupts the flow of the topic. Just add a reply and add the modified code.

    Jim

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by jimblumberg View Post
    Please don't modify your posts to change the code, it disrupts the flow of the topic. Just add a reply and add the modified code.

    Jim
    TRUE.If jim wouldn't have post,i wouldn't take notice of the updated code!

    EDIT->Does not the compiler gives you any errors?Mind that always when posting it is very helpful for other people to see what your compiler says

    For instance,this line of your code
    Code:
    cout << "Please enter your name:"  ";
    would produce an error like "expected ,or something like that.When you wish to write a " inside a message ,use the \

    In code this is what i am trying to say
    Code:
    cout << "Please enter your name:\"  ";
    Last edited by std10093; 09-02-2012 at 08:14 AM.

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    OK ty will do that next time this is our 1st assignment for this class I'm sure the code is right just don't know why it will let me enter my name,enter grades then give me the avg. of those 5 grades but then says press any button to continue without finishing the the division of the 2 whole numbers and its whole number with remainder.

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    Also I'm getting no errors at all

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You divide an int with a double.You may lose some precision but this is not a reason for the program to end.Can you copy paste the output you get?Because the code looks fine to me

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    here's my updated code to reflect my changes on the suggestions you have made
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    
    //declaring variables
        
    string name;
    
    
    
    double grade_1,grade_2,grade_3,grade_4,grade_5;
    
    double avg;
    
    
    //get input from user
    cout << "Please enter your name:\"  ";
    cin >> name;
    cout << endl;
    
    cout << "Enter first grade:\"  ";
    cin >> grade_1 ;
    if(grade_1<0)
    grade_1=0;
    cout << endl;
    
        cout << "Enter second grade:\"  ";
    cin >> grade_2 ;
    if(grade_2<0)
    grade_2=0;
    cout << endl;
    
    cout << "Enter third grade:\"  ";
    cin >> grade_3 ;
    if(grade_3<0)
     grade_3=0;
    cout << endl;
    
    cout << "Enter forth grade:\"  ";
    cin >> grade_4 ;
    if(grade_4<0)
      grade_4=0;
    cout << endl;
    
    cout << "Enter fifth grade:\"  ";
    cin >> grade_5 ;
    if(grade_5<0)
       grade_5=0;
    cout << endl;
    
    
    
    // Finding the avrage
    avg = (grade_1+grade_2+grade_3+grade_4+grade_5) /5 ;
    cout << "Your average is: " << avg;
    
    
    int firstNumber;
    
         double secondNumber;
    
    cout << "enter 2 whole numbers: " << endl; 
    cin >> firstNumber >> secondNumber;
    cout << firstNumber  << " divided by " << secondNumber
             << " is " << (firstNumber / secondNumber) << endl
             << "with a remainder of " << (firstNumber / secondNumber)
             cout << endl;
    
    return 0;

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    Ok now I am getting an errors one says error c2143 syntax error: missing';' before '<<'. and here again is my updated code as it stands.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    
    //declaring variables
        
    string name;
    
    
    
    double grade_1,grade_2,grade_3,grade_4,grade_5;
    
    double avg;
    
    
    //get input from user
    cout << "Please enter your name:\"  ";
    cin >> name;
    cout << endl;
    
    cout << "Enter first grade:\"  ";
    cin >> grade_1 ;
    if(grade_1<0)
    grade_1=0;
    cout << endl;
    
        cout << "Enter second grade:\"  ";
    cin >> grade_2 ;
    if(grade_2<0)
    grade_2=0;
    cout << endl;
    
    cout << "Enter third grade:\"  ";
    cin >> grade_3 ;
    if(grade_3<0)
     grade_3=0;
    cout << endl;
    
    cout << "Enter forth grade:\"  ";
    cin >> grade_4 ;
    if(grade_4<0)
      grade_4=0;
    cout << endl;
    
    cout << "Enter fifth grade:\"  ";
    cin >> grade_5 ;
    if(grade_5<0)
       grade_5=0;
    cout << endl;
    
    
    
    // Finding the avrage
    avg = (grade_1+grade_2+grade_3+grade_4+grade_5) /5 ;
    cout << "Your average is: " << avg;
    
    
    int firstNumber;
    
         double secondNumber;
    
    cout << "enter 2 whole numbers: " << endl; 
    cin >> firstNumber >> secondNumber;
    cout << firstNumber  << " divided by " << secondNumber
     << " is " << (firstNumber / secondNumber) << endl
     << "with a remainder of " << (firstNumber / secondNumber);
    cout << endl;
    
    return 0;

  15. #15
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    ok just figured it out myself thanks for the input guys!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. It wont display my string
    By laxkrzy in forum C Programming
    Replies: 4
    Last Post: 11-16-2010, 11:40 AM
  2. Parse A Part of String
    By Ali.B in forum C Programming
    Replies: 8
    Last Post: 08-07-2009, 03:07 PM
  3. Printing part of a string
    By AngKar in forum C Programming
    Replies: 9
    Last Post: 04-24-2006, 04:49 AM
  4. Copying part of a string
    By Aluvas in forum C++ Programming
    Replies: 2
    Last Post: 11-20-2002, 10:33 PM
  5. string cluster part 2
    By ronin in forum C Programming
    Replies: 2
    Last Post: 12-18-2001, 07:04 PM