how 2 use cin.get, cin.ignore in this program

November 22, 2010 at 10:18:41
Specs: Windows Vista
Hello,
I need help using cin.get and cin.ignore in my chars on the following program:

// Project 6-1

#include <iostream.h>

main()
{

int zip_code, phone_number;
char name[20], city[20], address[20],state[20];

cout << "Please enter your name max 20 characters and no spaces:";
cin >> name;
cout << "\r\n" "Please enter your address max 20 characters and no spaces:";
cin >> address;
cout << "\r\n" "Please enter your city max 20 characters and no spaces:";
cin >> city;
cout << "\r\n" "Please enter your your state max 20 characters and no spaces: ";
cin >> state;
cout << "\r\n" "Please enter your zip code max 20 characters and no spaces: " ;
cin >> zip_code;
cout << "\r\n" "Please enter your phone number max 20 characters and no spaces ";
cin >> phone_number;

cout << "\r\n";
cout << "(Name: " << name << ")";
cout << "\r\n";
cout << "(Address: " << address << ")";
cout << "\r\n";
cout << "(City: " << city << ")";
cout << "\r\n";
cout << "(State: " << state << ")";
cout << "\r\n";
cout << "(Zip Code: " << zip_code << ")";
cout << "\r\n";
cout << "(Phone Number: " << phone_number << ")";


return 0;
}

i need to be able to put spaces when I type my address and such.

thanks


See More: how 2 use cin.get, cin.ignore in this program

Report •


#1
November 22, 2010 at 10:24:53
do you mean when you read in data?

char array[80]
cin.getline(char_array, sizeof char_array, deliminator (defaults to '\n'))

string s
getline(cin, s);

while(cin.get() != '\n'){} // eat until '\n'


Report •

#2
November 22, 2010 at 14:09:26
yes I want to be able to put spaces when I enter my name, address etc. instead of 1235treeroad i should be able to put 1235 tree rd just copy and paste the corrected code please

thanks


Report •

#3
November 22, 2010 at 14:29:06
I think you can try or write the (tab char) after or before each cout <<

i didn't try it so you have to try it but i am giving you the idea ..

cout << endl;
cout << "(Name: " << name << ")";
cout << "\t";
cout << "(Address: " << address << ")";
cout << "\t";
cout << "(City: " << city << ")";
cout << "\t";
cout << "(State: " << state << ")";
cout << "\t";
cout << "(Zip Code: " << zip_code << ")";
cout << "\t";
cout << "(Phone Number: " << phone_number << ")";
i hope it will work


Report •

Related Solutions


Ask Question