What's Here?
- Members: 340,161
- Replies: 920,579
- Topics: 154,962
- Snippets: 4,855
- Tutorials: 1,257
- Total Online: 4,031
- Members: 127
- Guests: 3,904
|
Welcome to Dream.In.Code |
|
|
Become an Expert!
Join 340,161 Programmers for FREE! Get instant access to thousands  of experts, tutorials, code snippets, and more! There are 4,031 people online right now. Registration is fast and FREE... Join Now!
Chat LIVE With a Expert
|
program to calculate the grade of a student
program to calculate the grade of a student
Rate Topic:
   
Posted 11 April 2009 - 05:14 AM
#include<iostream.h>
#include<conio.h>
int main()
{
char fname[20];
char lname[15];
char grade;
int age;
cout<<"what is your first name?"<<endl;
cin.get(fname,20);
cout<<"what is your last name?"<<endl;
cin.get(lname,15);
cout<<"what letter grade do you deserve?"<<endl;
cin>>grade;
grade--;
cout<<"what is your age?"<<endl;
cin>>age;
cout<<"name:"<<lname<<","<<fname<<endl;
cout<<"grade:"<<grade<<endl;
cout<<"age:"<<age<<endl;
getch();
return 0;
}
when the program runs it skips lname i don't know why
*** MOD EDIT: Added code tags. Please  ***
This post has been edited by JackOfAllTrades: 11 April 2009 - 05:58 AM
Posted 11 April 2009 - 05:52 AM
Welcome to DIC!
1 - Please
2 - Why are you using <iostream.h> instead of <iostream>?
3 - Why no line using namespace std; at the top of your code?
4 - I'd suggest replacing getch(); with getchar();
5 - I'd suggest using C++ strings in place of C-style array strings like char fname[20];. Read more here
http://www.cplusplus...ference/string/
6 - I'd suggest replacing your calls to cin.get() with getline(). Read more here
http://www.cplusplus...string/getline/
Was This Post Helpful?
1
Posted 11 April 2009 - 07:24 AM
janotte, on 11 Apr, 2009 - 05:52 AM, said:
Welcome to DIC!
1 - Please
2 - Why are you using <iostream.h> instead of <iostream>?
3 - Why no line using namespace std; at the top of your code?
4 - I'd suggest replacing getch(); with getchar();
5 - I'd suggest using C++ strings in place of C-style array strings like char fname[20];. Read more here
http://www.cplusplus...ference/string/
6 - I'd suggest replacing your calls to cin.get() with getline(). Read more here
http://www.cplusplus...string/getline/
Posted 11 April 2009 - 08:29 AM
gege, you've posted janotte's reply twice and nothing else...what is it you're trying to do?
Posted 11 April 2009 - 10:30 AM
Got rid of extra post with no new content.

- New D.I.C Head
-
-
Group:
Members
-
Posts:
39
-
Joined:
06-March 09
Dream Kudos: 0
Posted 11 April 2009 - 03:12 PM
gege, on 11 Apr, 2009 - 05:14 AM, said:
#include<iostream.h>
#include<conio.h>
int main()
{
char fname[20];
char lname[15];
char grade;
int age;
cout<<"what is your first name?"<<endl;
cin.get(fname,20);
cout<<"what is your last name?"<<endl;
cin.get(lname,15);
cout<<"what letter grade do you deserve?"<<endl;
cin>>grade;
grade--;
cout<<"what is your age?"<<endl;
cin>>age;
cout<<"name:"<<lname<<","<<fname<<endl;
cout<<"grade:"<<grade<<endl;
cout<<"age:"<<age<<endl;
getch();
return 0;
}
when the program runs it skips lname i don't know why
*** MOD EDIT: Added code tags. Please  ***
Put a cin.ignore() before it.
eg
#include<iostream.h>
#include<conio.h>
int main()
{
char fname[20];
char lname[15];
char grade;
int age;
cout<<"what is your first name?"<<endl;
cin.get(fname,20);
cout<<"what is your last name?"<<endl;
cin.ignore();
cin.get(lname,15);
cout<<"what letter grade do you deserve?"<<endl;
cin>>grade;
grade--;
cout<<"what is your age?"<<endl;
cin>>age;
cout<<"name:"<<lname<<","<<fname<<endl;
cout<<"grade:"<<grade<<endl;
cout<<"age:"<<age<<endl;
getch();
return 0;
}

- New D.I.C Head
-
-
Group:
Members
-
Posts:
39
-
Joined:
06-March 09
Dream Kudos: 0
Posted 11 April 2009 - 03:24 PM
Ryan2, on 11 Apr, 2009 - 03:12 PM, said:
gege, on 11 Apr, 2009 - 05:14 AM, said:
#include<iostream.h>
#include<conio.h>
int main()
{
char fname[20];
char lname[15];
char grade;
int age;
cout<<"what is your first name?"<<endl;
cin.get(fname,20);
cout<<"what is your last name?"<<endl;
cin.get(lname,15);
cout<<"what letter grade do you deserve?"<<endl;
cin>>grade;
grade--;
cout<<"what is your age?"<<endl;
cin>>age;
cout<<"name:"<<lname<<","<<fname<<endl;
cout<<"grade:"<<grade<<endl;
cout<<"age:"<<age<<endl;
getch();
return 0;
}
when the program runs it skips lname i don't know why
*** MOD EDIT: Added code tags. Please  ***
Put a cin.ignore() before it.
eg
#include<iostream.h>
#include<conio.h>
int main()
{
char fname[20];
char lname[15];
char grade;
int age;
cout<<"what is your first name?"<<endl;
cin.get(fname,20);
cout<<"what is your last name?"<<endl;
cin.ignore();
cin.get(lname,15);
cout<<"what letter grade do you deserve?"<<endl;
cin>>grade;
grade--;
cout<<"what is your age?"<<endl;
cin>>age;
cout<<"name:"<<lname<<","<<fname<<endl;
cout<<"grade:"<<grade<<endl;
cout<<"age:"<<age<<endl;
getch();
return 0;
}
Its better to just use cin.getline() though
1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users
|
Be Social
Programming
Web Development
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|