-
November 5th, 2010, 12:54 AM
#1
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
-
November 5th, 2010, 02:39 AM
#2
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.
-
November 5th, 2010, 05:39 AM
#3
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 colonWelcome 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.
-
November 5th, 2010, 06:25 AM
#4
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.
-
November 5th, 2010, 07:46 AM
#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.
-
November 5th, 2010, 08:47 AM
#6
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;
-
November 5th, 2010, 08:53 AM
#7
Re: C++ Programming Help Need Urgently
system("pause");//before return 0;
-
November 5th, 2010, 08:57 AM
#8
Re: C++ Programming Help Need Urgently
Originally Posted by Morbane
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:
-
November 5th, 2010, 09:04 AM
#9
Re: C++ Programming Help Need Urgently
Originally Posted by Moschops
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:
He means that the system("pause"); will not work on some command line compilers - or on other OS's - eg Linux or the like.
-
November 5th, 2010, 09:59 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|