CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2010
    Posts
    5

    Lightbulb C++ Programming Help Need Urgently

    Hi everybody,

    I need a little help in my university assignment of C++ Programming.
    I have to make a programme which should takes 10 inputs of grades and will count the total no of 'A' grades and 'B' grades and shows the result.
    Inputs would be taken as character 'A' and 'B' only and using while or do/while loop.
    This is my code.

    Code:
    /*tnos = total no of students
       grad = grades
       ga = total of grades a
       gb = total of grades b*/
    
    #include <iostream.h>
    using namespace std;
    int main()
    {
          char grad;
          int tnos, ga, gb;
          tnos = 1;
          ga = 0;
          gb = 0;
          do
          {
                     cout<<"Enter the Grade of Student "<<tnos<<" :";
                     cin>>grad;
                     if(grad=='a')
                     {
                             ga=ga+1;
                             tnos=tnos+1;
                             }
                     else if(grad=='b')
                     {
                                  gb=gb+1;
                                  tnos=tnos+1;
                                  }
                     else
                     {
                         cout<<"Please enter only gra A or B:";
                         }
                         }
                         while(tnos<=10):
                         {
                                         cout<<"Total A grades are : "<<ga;
                                         }                  
                     }
    but my code is worked till loop and end after it. i.e. it gets the codes right but did not shows any result and close the cmd windows.

    Please help me urgently today is the last date od submission. I found this forum after a long search so please help.

    Thanx to all

  2. #2
    Join Date
    Oct 2010
    Posts
    68

    Re: C++ Programming Help Need Urgently

    It shows the results after your cout but then the program finishes and closes before you can see it. You need to add something to pause the program after the last cout in order to see what it says.

    EDIT: Also, there is a colon where a semicolon should be...and you could remove some unnecessary brackets.
    Last edited by Austin.Soucy; November 5th, 2010 at 02:43 AM.

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: C++ Programming Help Need Urgently

    To prevent the window to close either start the program by using ctrl+F5 or set a breakpoint on the closing brace of main.

    I do not recommend to remove any "unnecessary" brackets. In my opinion it's a common mistake to think that writing anything more than abolutely necessary is a sign of not being a professional. I believe the opposite. The code should be written/formatted to be easy to read, understand and maintain, even for a beginner. Omitting braces makes it more likely that a beginner forget to add on when needed. Omitting parenthesis that aren't needed makes it more likely a beginner misunderstand the statement and so on.

    What you should do is to fixup your indentation (cut&paste effect?) and remove this colon
    Code:
     while(tnos<=10):
    Welcome to codeguru and a big applause for using code tags in your very first post!
    Last edited by S_M_A; November 5th, 2010 at 05:42 AM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Nov 2010
    Posts
    5

    Re: C++ Programming Help Need Urgently

    Thank you guys thank you so much i ties than will b back if i got any problems. Thanks again.

  5. #5
    Join Date
    Nov 2010
    Posts
    5

    Re: C++ Programming Help Need Urgently

    This is the full of code, can you tells me that what should me need to change for proper execution
    Code:
    #include <iostream.h>
    using namespace std;
    main()
    {                                     //Initialization of veriables
          char grad;                     //to take inputs of grades A or B
          int tnos, ga, gb;              
          tnos = 1;                      //Total number of students
          ga = 0;                        //Total number of grad 'a'
          gb = 0;                        //Total number of grad 'b'
          do
          {
               cout<<"Enter the Grade of Student "<<tnos<<" :";    //to take inputs
                     cin>>grad;
                     if(grad=='a')            //to decide while the grade is 'a', 'b' or anyother
                     {
                             ga=ga+1;            //counter to count 'a' grades
                             tnos=tnos+1;
                             }
                     else if(grad=='b')
                     {
                                  gb=gb+1;          //counter to count 'b' grades
                                  tnos=tnos+1;
                                  }
                     else
                     {
                         cout<<"Please enter only grads A or B:";
                         }
                         }
                         while(tnos<=10);               //maximum inputs to take or total number of students whos grades are required
                                    {
                                         cout<<"Total A grades are : "<<ga;
                                         cout<<"Total B grades are : "<<gb;
                                         }
                                         
                                         //Now on the basis of total number of grades we shell show the status of a class.
                                         
                                         if(ga<=2)
                                         cout<<"Your class is Poor..!";
                                         else if(ga<=7)
                                         cout<<"Your class is Good..!";
                                         else
                                         cout<<"Your class is Brilliant..!";
                     }
    it takes the input correctly but close cmd window before showing the result.

  6. #6
    Join Date
    Apr 2008
    Posts
    118

    Re: C++ Programming Help Need Urgently

    There's nothing inherently wrong with your code that makes the window close - you're running it in the wrong way to see your results.

    I assume you're using some kind of windows machine. Open a command line (probably hidden under "Accessories" if I recall correctly- the last Win machine I used was XP, so things have probably moved around a bit since my day) and run the program from there.

    That means that you will have to use the command "cd" to change directory to where the programme has been put, and then type the name of the programme. And then press enter. If you do not know the name of the programme, or you do not know where it is, you need to learn more about the tools you're using, but that's a different problem. If the idea of using a command line to change directory means nothing to you, you need to learn more about your operating system, but that's a different problem.

    If you cannot do the above, you can alter your programme to make it wait at the end. There are many, many ways to do this. Here is one.

    Code:
    int z;
    cout << "Press enter to finish.";
    cin >> z;

  7. #7
    Join Date
    Oct 2010
    Location
    Australia
    Posts
    98

    Re: C++ Programming Help Need Urgently

    system("pause");//before return 0;

  8. #8
    Join Date
    Apr 2008
    Posts
    118

    Re: C++ Programming Help Need Urgently

    Quote Originally Posted by Morbane View Post
    system("pause");//before return 0;
    If you choose to do it this way, don't forget to use an operating system that supports this (if you're using windows it probably does), don't expect your code to be portable, and add a new header file to the top:

    Code:
    #include <cstdlib>

  9. #9
    Join Date
    Oct 2010
    Location
    Australia
    Posts
    98

    Re: C++ Programming Help Need Urgently

    Quote Originally Posted by Moschops View Post
    If you choose to do it this way, ... don't expect your code to be portable, and add a new header file to the top:

    Code:
    #include <cstdlib>
    He means that the system("pause"); will not work on some command line compilers - or on other OS's - eg Linux or the like.

  10. #10
    Join Date
    Nov 2010
    Posts
    5

    Re: C++ Programming Help Need Urgently

    Thanks " Morbane" and "Moschops" for help. Really thank you very very much.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured