Need some help here

// i wanna know why case 1 is not working ?
//and how i can make it working
//hope find an answer fast
// how i can make (cin) fun work well ?


#include<iostream>
#include<string>
#include<sstream>
#include<cstring>
using namespace std;
struct data{
string name;
long int id;
long int balance;
};
int main()
{
string c,b,y,u;
bool z;
long int d,dep;
int x,i,n,f,j=1,p=1,v;
string temp;
data w[50];
w[1].name="e1 f1";
w[1].id=29;
w[1].balance=3000;
w[2].name="e2 f2";
w[2].id=30;
w[2].balance=3000;
w[3].name="e3 f3";
w[3].id=31;
w[3].balance=2500;
w[4].name="e4 f4";
w[4].id=294042100;
w[4].balance=101;
w[5].name="e5 f5";
w[5].id=294061400;
w[5].balance=500;
for( ; ; )
{
cout<<" Please Choose Number of Operation u want to Do....."<<"\n"<<endl;
cout<<"1. Entering Record"<<endl;
cout<<"2. Searching the Record"<<endl;
cout<<"3. Deleting the Record"<<endl;
cout<<"4. Modification of Records"<<endl;
cout<<"5. Banking operation"<<endl;
cout<<"6. Data entry validation"<<endl;
cout<<"7. Print Clients List"<<endl;
cout<<endl;
cout<<"Number of operation is ";
cin>>x;
//getline(cin,temp);
//stringstream(temp)>>x;
//cin.get();
switch(x)
{
case 1 :
cout<<"\n Entering Record"<<endl;
cout<<"\n"<<endl;
cout<<"Enter Number of Clients That you want to ADD"<<endl;
cin>>n;
for(i=1; i<=n; i++)
{
cout<<"Please Enter Client Name :"<<endl;
getline(cin,w[5+i].name);
cin.get();
cout<<"Please Enter Client ID :"<<endl;
getline(cin,temp);
stringstream(temp)>>w[5+i].id;
cin.get();
cout<<"Please Enter Client INITIAL BALANCE :"<<endl;
getline(cin,temp);
stringstream(temp)>>w[5+i].balance;
cin.get();
}
cout<<endl;
cout<<"the new Record saved \3\3\3 thx \3\3\3"<<endl;
cout<<"\n"<<" (^__^)(^__^)\n"<<endl;
break;
case 2 :
cout<<"\n \2\2\2\2\2\2\2 Searching the Record \2\2\2\2\2\2\2 \n\n"<<endl;
break;
case 3 :

break;
case 4 :
break;
case 5 :

break;
case 6 :
break;
default :
cout<<"\n Wrong Entery \n"<<endl;
break;
}
}
return 0;
}
Last edited on
closed account (3CXz8vqX)
Wrap with Code tags please.

Where's your break; statement? Found it.
Last edited on
is a right code but compiler doing a strange things
i dont know if the problem on ---> cin or what
Last edited on
closed account (3CXz8vqX)
Actually it works for me... which compiler are you using?

Edit. Dunno, can't replicate the problem. As I said, the code works completely and prints " Entering Record" complete with the extra space on the left.
Last edited on
i use visual studio 2010
yes it works
but when the user enter the info
compilor dont put it on it's memory
and i find the loop looks for another time with out i enter which case i want
Last edited on
closed account (3CXz8vqX)
That's not really a compiler that's an IDE. The syntax all checks out but I have a feeling that Visual have daft header requirements...
code works with u good ?
it dont do this ----> when the user enter the info
compilor dont put it on it's memory
and i find the loop looks for another time with out i enter which case i want
You need to remove the newline character which remains in the input buffer after cin >> n;
1
2
3
    cout<<"Enter Number of Clients That you want to ADD"<<endl;
    cin>>n;
    cin.get();

And conversely, you don't need cin.get(); after the getline operation.
will try it now
it's not works good :(( when i delete cin.get();
now it work please enter record name
and please enter record id together
it dont want to take the info
_________________ No one know the answer ?
Last edited on
Thanks chervil it works but..
what is cin.get(); do?
Last edited on
what is cin.get(); do?
It reads a single character. In this case, we ignore the result, so cin.ignore() might be better, as the name better describes what we need.

After the user types some input in the keyboard, they press the "enter" key.
That stores all of the input, including a newline character, in the input buffer.
Some commands, such as cin>>n; will leave that newline sitting in the buffer.

When the next getline() is executed, it reads everything up to the newline as the required input. But because the very first character is a newline, the getline simply reads an empty string, and then moves on, meaning the user never even has a chance to type anything at all.

The solution is to remove any characters from the buffer before the getline. That's what the cin.get is for.
Last edited on
Topic archived. No new replies allowed.