School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
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!



program to calculate the grade of a student

program to calculate the grade of a student Rate Topic: -----

#1 gege  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 7
  • Joined: 07-April 09


Dream Kudos: 0

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 :code: ***

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

Was This Post Helpful? 0
  • +
  • -


#2 janotte  Icon User is offline

  • code > sword
  • Icon
  • Group: Expert w/DIC++
  • Posts: 2,675
  • Joined: 28-September 06


Dream Kudos: 0

Expert In: C/C++

Posted 11 April 2009 - 05:52 AM

Welcome to DIC!

1 - Please :code:

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

#3 gege  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 7
  • Joined: 07-April 09


Dream Kudos: 0

Posted 11 April 2009 - 07:24 AM

View Postjanotte, on 11 Apr, 2009 - 05:52 AM, said:

Welcome to DIC!

1 - Please :code:

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? 0
  • +
  • -

#4 Pwn  Icon User is offline

  • D.I.C Regular
  • PipPipPip
  • Group: Members
  • Posts: 380
  • Joined: 25-November 07


Dream Kudos: 0

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?
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is online

  • Mayor of Simpleton
  • Icon
  • View blog
  • Group: Moderators
  • Posts: 7,132
  • Joined: 23-August 08


Dream Kudos: 50

Expert In: Being annoyed with lazy people.

Posted 11 April 2009 - 10:30 AM

Got rid of extra post with no new content.
Was This Post Helpful? 0
  • +
  • -

#6 Ryan2  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 39
  • Joined: 06-March 09


Dream Kudos: 0

Posted 11 April 2009 - 03:12 PM

View Postgege, 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 :code: ***


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


Was This Post Helpful? 0
  • +
  • -

#7 Ryan2  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 39
  • Joined: 06-March 09


Dream Kudos: 0

Posted 11 April 2009 - 03:24 PM

View PostRyan2, on 11 Apr, 2009 - 03:12 PM, said:

View Postgege, 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 :code: ***


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
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

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

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month