Back | Reverse | Quick Reply | Post Reply |

Need help in this source code C++
Link | by jansuke on 2009-03-10 19:22:48 (edited 2009-03-10 20:44:29)
the main problem is that when i type in my whole name it goes berserk...

Enter employee's name:michael jan yamasaki

Enter employee's monthly salary:

The bonus you will be receiving this month is :-4.62798e+061
Your total salary this month is :-1.38839e+062
Press any key to continue


but when i type in short names it works...

Enter employee's name:michael

Enter employee's monthly salary:7000


The bonus you will be receiving this month is :7000
Your total salary this month is :14000
Press any key to continue


can anyone help me? Our professor wont even teach us -_-... she just fly away whenever she enters the room, so we just studied the book by ourselves(and yeah i suck... we use visual studio 2003)...

heres the code... i didnt put the if else statement because it gives b tag is not closed at position 1103!
#include <iostream.h>
int main()
{
	double b,ts,s;
	char n[20];
	cout<<"Enter employee's name:";
	cin>>n;
	cout<<"nEnter employee's monthly salary:";
	cin>>s;
	if (s > 6999)
	return 0;
}

Re: Need help in this source code C++
Link | by gendou on 2009-03-10 20:45:26 (edited 2009-03-10 20:45:52)
You should use cin.getline() instead of >> to load the employee's name into the variable n.

See: http://www.cplusplus.com/reference/iostream/istream/getline.html


Re: Need help in this source code C++
Link | by Lanster on 2009-03-12 12:11:41
char (variable)[20] -> that variable can store up to 20 characters
char (variable)[256] -> that variable can store up to 256 characters

Reinforce

Re: Need help in this source code C++
Link | by Jiraiya on 2009-06-26 23:48:13
Of course he could just use the string class and be done with character arrays altogether. He can also set the noskipws flag to be able to use the << operator to grab such input.

Back | Reverse | Quick Reply | Post Reply |

Copyright 2000-2024 Gendou | Terms of Use | Page loaded in 0.0021 seconds at 2024-12-30 12:40:36