查看: 773|回复: 3
|
奇怪的getline...在C++
[复制链接]
|
|
不知道为什么在function里面就get不到,在int main()
就get 得到,有谁知道为什么吗?
#include<iostream>
#include<string>
#include<cmath>
#include<fstream>
#include<cstdlib>
#include<cstring>
#include<conio.h>
#include<iomanip>
using namespace std;
int password();
int username();
void add();
int view();
int edit();
int print();
int unsuccessful=0;
int main()
{
char menu;
cout<<"Welcome To David's Evolution Company"<<endl;
username();
cout<<"\nYou Have successful Login Evolution Company"<<endl;
cout<<"\n\n\nProduct Menu\n------------"<<endl;
cout<<"A--dd \t\tNew Product\n";
cout<<"V--iew \t\tA Product By Code\n";
cout<<"E--dit \t\tA Product Data\n";
cout<<"P--rint \tProduct\n";
cout<<"\nPlease Select Which Function You Want:";
cin>>menu;
if(menu=='A'||menu=='a')
add();
else
cerr<<"Invalid Input"<<endl;
return 0;
}
//function to checking username
int username()
{
int ch=0;
char user_in[50];
char read[50];
ifstream read_pass("username.txt");
if(read_pass.fail())
cerr<<"ERROR";
a1: cout<<"Username:";
fflush(stdout);
int i=0;
cin.get(user_in,50);
while(read_pass.getline(read, 50))
{
if(strcmp(user_in,read))
{
ch=0;
}
else
{
ch=1;
break;
}
}
if(ch==1)
{
password();
}
else
{
char confirm;
cout<<"\nIncorrect Username\n\n";
++unsuccessful;
while(unsuccessful<3){
cout<<"Do You Want To Try Again?(Y/N)\n";
cin>>confirm;
cin.ignore(); //
if(confirm=='Y'||confirm=='y')
goto a1;//username();
else if(confirm=='N'||confirm=='n'){
cout<<"Thank You\n";
exit(1);}
else
{
cout<<"Why You So Naughty,Never Follow The Instruction,\nNow You Cannot Continue,HAHA,ByeBye"<<endl;
exit(1);
}
}
if(unsuccessful>=3){
cerr<<"You Have Been Enter Wrong Username 3 Times\n";
cerr<<"Exit Program\n";
exit(1);
}
}
return 0;
}
//function to checking password
int password()
{
int ch;
char confirm;
char user_in[50];
char read[50];
ifstream read_pass("pass.txt");
if(read_pass.fail())
cerr<<"ERROR";
b1:cout<<"Password:";
fflush(stdout);
int i=0;
while ((ch = getch()) != EOF && ch != '\n' && ch != '\r') //&&i<10)
{
if (ch == '\b' && i > 0)
{
cout<<"\b \b";
fflush(stdout);
i--;
user_in[i] = '\0';
}
else if (isalnum(ch))
{
putchar('*');
user_in[i++] = (char)ch;
}
}
user_in[i] = '\0';
read_pass.getline(read, 50);
if(strcmp(user_in,read)){
cout<<"\nWrong Password\n";
++unsuccessful;
while(unsuccessful<3){
cout<<"Do You Want To Try Again?(Y/N)\n";
cin>>confirm;
cin.ignore(); //
if(confirm=='Y'||confirm=='y')
goto b1;//username();
else if(confirm=='N'||confirm=='n'){
cout<<"Thank You\n";
exit(1);}
else
{
cout<<"Who Ask You Not Follow The Instruction,\nNow You Cannot Continue,BYEBYE"<<endl;
exit(1);
}
}
if(unsuccessful>=3){
cerr<<"You Have Been Enter Wrong Password 3 Times\n";
cerr<<"Exit Program\n";
exit(1);
}
read_pass.seekg(0);
read_pass.clear();
}
return 0;
}
void add()
{
ofstream output;
output.open ("product.txt");
char name[80];
cout<<"Please Enter Product Name\n";
cin.getline (name,80);
cout<<name;
output<<name;
output.close();
}
问题处在add()function,get 不到... |
|
|
|
|
|
|
|
发表于 11-3-2006 12:29 AM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 11-3-2006 12:46 AM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 11-3-2006 12:03 PM
|
显示全部楼层
我的getline 问题是
cin>>a;
cin.getline(a,80);
我打david I love u,
他只能getline i love u,
当我只放cin.getline(a,80)
就get不到任何东西,因为没有机会输入...
不知道为什么在int main()
又可以get 到...
真奇怪... |
|
|
|
|
|
|
| |
本周最热论坛帖子
|