C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 306,814 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,687 people online right now. Registration is fast and FREE... Join Now!




program to calculate the grade of a student

 

program to calculate the grade of a student

gege

11 Apr, 2009 - 05:14 AM
Post #1

New D.I.C Head
*

Joined: 7 Apr, 2009
Posts: 6


My Contributions
cpp

#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 code.gif ***

This post has been edited by JackOfAllTrades: 11 Apr, 2009 - 05:58 AM

User is offlineProfile CardPM
+Quote Post


janotte

RE: Program To Calculate The Grade Of A Student

11 Apr, 2009 - 05:52 AM
Post #2

code > sword
Group Icon

Joined: 28 Sep, 2006
Posts: 2,171



Thanked: 153 times
Expert In: C/C++

My Contributions
Welcome to DIC!

1 - Please code.gif

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.com/reference/string/

6 - I'd suggest replacing your calls to cin.get() with getline(). Read more here
http://www.cplusplus.com/reference/string/getline/
User is offlineProfile CardPM
+Quote Post

gege

RE: Program To Calculate The Grade Of A Student

11 Apr, 2009 - 07:24 AM
Post #3

New D.I.C Head
*

Joined: 7 Apr, 2009
Posts: 6


My Contributions
QUOTE(janotte @ 11 Apr, 2009 - 05:52 AM) *

Welcome to DIC!

1 - Please code.gif

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.com/reference/string/

6 - I'd suggest replacing your calls to cin.get() with getline(). Read more here
http://www.cplusplus.com/reference/string/getline/


User is offlineProfile CardPM
+Quote Post

Pwn

RE: Program To Calculate The Grade Of A Student

11 Apr, 2009 - 08:29 AM
Post #4

D.I.C Regular
***

Joined: 25 Nov, 2007
Posts: 361



Thanked: 13 times
My Contributions
gege, you've posted janotte's reply twice and nothing else...what is it you're trying to do?
User is offlineProfile CardPM
+Quote Post

JackOfAllTrades

RE: Program To Calculate The Grade Of A Student

11 Apr, 2009 - 10:30 AM
Post #5

I exist to Google your problems.
Group Icon

Joined: 23 Aug, 2008
Posts: 5,311



Thanked: 453 times
Dream Kudos: 50
Expert In: Being annoyed with lazy people.

My Contributions
Got rid of extra post with no new content.
User is offlineProfile CardPM
+Quote Post

Ryan2

RE: Program To Calculate The Grade Of A Student

11 Apr, 2009 - 03:12 PM
Post #6

New D.I.C Head
*

Joined: 6 Mar, 2009
Posts: 39

QUOTE(gege @ 11 Apr, 2009 - 05:14 AM) *

cpp

#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 code.gif ***


Put a cin.ignore() before it.

eg
CODE

#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;
}

User is offlineProfile CardPM
+Quote Post

Ryan2

RE: Program To Calculate The Grade Of A Student

11 Apr, 2009 - 03:24 PM
Post #7

New D.I.C Head
*

Joined: 6 Mar, 2009
Posts: 39

QUOTE(Ryan2 @ 11 Apr, 2009 - 03:12 PM) *

QUOTE(gege @ 11 Apr, 2009 - 05:14 AM) *

cpp

#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 code.gif ***


Put a cin.ignore() before it.

eg
CODE

#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

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/20/09 10:03PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month