0
#include<iostream>
#include<string>
#include<cstring>

using namespace std;
void cpp_string();
void cstyle_string();

int main()
{
  cpp_string();
  cstyle_string();

  system("pause");
  return 0;
}

void cpp_string()
{
  string fName, lName;
  char grade;
  int age;

  cout << "What is your first name?";
  getline(cin, fName);
  cout << "What is your last name?";
  getline(cin, lName);
  cout << "What letter grade do you deserve?";
  cin >> grade;
  cout << "What is your age?";
  cin >> age;

  cout << "Name: " << fName << ", " << lName << endl;
  cout << "Grade: " << grade << endl;
  cout << "Age: " << age << endl;

  return;
}

void cstyle_string()
{
  char fNm[20], lNm[20];
  char grade;
  int age;

  cout << "What is your first name?";
  cin.get(fNm, 20).get();
  cin.clear();
  cout << "What is your last name?";
  cin.get(lNm, 20).get();
  cout << "What letter grade do you deserve?";
  cin >> grade;
  cout << "What is your age?";
  cin >> age;

  cout << "Name: " << fNm << ", " << lNm << endl;
  cout << "Grade: " << grade << endl;
  cout << "Age: " << age << endl;

  return;
}

I'm getting output as

What is your first name?demiurge conon  
What is your last name?no  
What letter grade do you deserve?a  
What is your age?22  
Name: demiurge conon, no  
Grade: a  
Age: 22  
What is your first name?What is your last name?What letter grade do you deserve?What is your age?Name: ,  
Grade: ╠ 
Age: -858993460 
Press any key to continue . . . 

but if I run cstyle_string() in different file then I'm not getting any errors code works perfectly.

I want to know why this is happening?

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • 1
    You are calling both `cpp_string()` and `cstyle_string()`. Call only one at a time. Because they consume the input. – rustyx Dec 19 '17 at 08:42
  • They both run at different times....I mean first cpp_string() will run then after it returns, cstyle_string() will run. It worked well when i didn't use cin.get() in cpp_string(). Before this, i used cin>>fname for input and it worked well but it only takes a single word so i used get() to get a line of characters. Now code not working. – demiurge conon Dec 19 '17 at 08:53

1 Answers1

1

There are two question.

  1. Redundant \n

  2. the state of cin

The last cin in cpp_string is cin >> age.

It will leave a \n not extracted.

In first of cstyle_string is cin.get(fNm, 20).get();

The delimiting character is not extracted from the input sequence if found, and remains there as the next character to be extracted from the stream

the cin.get(FNm, 20) will parse empty input before \n, and no characters are available in the stream in actually. In this case, the failbit flag will be set and next all cin >> operator will fail.

You can only call cstyle_string and press enter directly, the same thing will happen.

neuo
  • 653
  • 4
  • 10
  • Thanks, new line has to be ignored. so used this line cin.ignore(numeric_limits::max(),'\n'); before first cout in cstyle_string(), now it's working fine. – demiurge conon Dec 19 '17 at 10:06