See the below code. If i give a "space" input for variable name, another integer variable is not being stored in the file. Why?
I've posted the outputs too.
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
int age;
char name[100];
ofstream obj,obj10;
obj.open("lol.dat", ios::trunc);
cout<<"Enter Name"<<endl;
cin.getline(name,100);
cin.ignore();
obj<<name<<endl;
cout<<"Enter age"<<endl;
cin>>age;
cin.ignore();
obj10<<age<<endl;
obj.close();
//read
ifstream obj2;
obj2.open("lol.dat");
obj2>>name;
cout<<"Name:"<<name<<endl;
obj2>>age;
cout<<"Age:"<<age<<endl;
obj2.close();
return 0;
}
My outputs:
Enter Name
Ankith
Enter age
1234
Name:Ankith
Age:1234
Enter Name
Ankith Prabhas
Enter age
12345
Name:Ankith
Age:0
What do i need to do to take the complete name "Ankith Prabhas" as one input and another integer?
Try to use getline() in strings where you expect spaces.
Related
This question already has answers here:
std::cin input with spaces?
(8 answers)
Closed 1 year ago.
I am making something where you have to put your name but my string doesn't save the spaces, why?
#include <iostream>
using namespace std;
string name;
int main()
{
cout<<"<System>: Please enter your name"<<endl;
cin>>name;
cout<<name;
return 0;
}
I entered:
Test 123
And I got:
Test
the insertion operator inserts only the first string (before any whitespace) from std::cin to the string if you want to take the whole line use std::getline()
#include <iostream>
using namespace std;
string name;
int main()
{
cout<<"<System>: Please enter your name"<<endl;
std::getline(std::cin, name);
cout<<name;
return 0;
}
And see this Why is "using namespace std;" considered bad practice?
std::cin << only reads up to the next space (ie. space, tabulation or line break). If you want to read the whole line, you will have to use std::getline. Moreover, unless you have a very clear reason, you should declare the variable name as a local variable of the function main.
#include <iostream>
using namespace std;
int main()
{
string name;
cout<<"<System>: Please enter your name"<<endl;
getline(cin, name);
cout<<name;
return 0;
}
cin takes any whitespace (spaces, tabs, etc.) as a terminating character which is why you're only getting the first word.
Using getline() you can get the whole sentence until the return key is pressed:
int main()
{
cout<<"<System>: Please enter your name"<<endl;
getline(cin, name);
cout<<name;
return 0;
}
This question already has an answer here:
How to make cin work after using getline?
(1 answer)
Closed 3 years ago.
This program is a small hospital management program.
It works with C++ and uses binary files and classes.
it is a simple prog that taken the input of patients details and saves in a binary file, and displays the patients details by searching regno.
The code does not run beyond : cin>>A1.PI.age; and starts printing endless loop of something.
PLEASE REPLY ME ON WHERE HAVE I GONE WRONG
here is the code:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
class all
{ private:
struct address
{ int house;
char street[30];
char city[30];};
struct patient_info
{ char name[40];
address AD1;
int age;
int maritalstatus;
int regno;
char bldgrp[3];
char sex;
}PI;
int task;
protected:
void firstpinfo();
void showpinfo();
void enterpinfo();
public:
void tasks();
char ch;
int serial;
}A1;
class date :public all
{ private :
int day;
int month;
int year;
public:
void enterdate()
{cout<<"enter day of date";
cin>>day;
cout<<"enter month";
cin>>month;
cout<<"enter year";
cin>>year;
}
void showdate()
{ cout<<day<<"/"<<month<<"/"<<year;}
}D1;
//global variables
int count,attempt;
void main()
{ count=0;
cout<<" HOSPITAL MANAGEMENT SOFTWARE
";
D1.enterdate();
A1.tasks();
getch();
while(count==0)
{ A1.tasks();
cout<<"press 0 to continue and 1 to exit";
cin>> count;
}
getch();
}
void all::tasks()
{ attempt=0;
D1.showdate();
cout<<"select task"<<endl
<<"1.show patient details"<<endl
<<"2.enter details of a patient"<<endl
<<"3.exit prog"<<endl;
cin>>task;
switch(task)
{
case 1: {cout<<"enter regno to display"<<endl;
int search;
cin>>search;
fstream fon;
fon.open("hospital.dat",ios::in|ios::binary);
if(!fon)
{cout<<"error in opening"<<endl;
getch();
exit(0);
}
else
{fon.read((char*)&A1,sizeof(A1));
if(A1.PI.regno==search)
{cout<<"showing details";
A1.showpinfo();}
else
{cout<<"regno not found";}
fon.close();
}
break;}
case 2: {cout<<"adding a new patient";
A1.enterpinfo();
fstream fan;
fan.open("hospital.dat",ios::in|ios::binary);
if(fan)
{fan.write((char*)&A1,sizeof(A1));}
fan.close();
break;}
case 3: { cout<<"exiting...press any key";
getch();
exit(0);
break;
}
default: {cout<<"error... press anykey to try again";
getch();
A1.tasks();
};
}}//END OF TASKS
void all::showpinfo()
{cout<<"patient regno\n"<<A1.PI.regno<<endl;
cout<<"patient name\n"<<A1.PI.name<<endl;
cout<<"address of patient\n"<<A1.PI.AD1.house<<" "<< PI.AD1.street<<" "<<PI.AD1.city<<endl;
cout<<"blood group"<<A1.PI.bldgrp<<endl;
cout<<"sex"<<A1.PI.sex<<endl;
cout<<"data extracted";
}
void all:: enterpinfo()
{
cout<<"enter unique registration number";
cin>>PI.regno;
cout<<"enter patient name"<<endl;
cin.getline(A1.PI.name,50);
cout<<"enter address( house, street, city)"<<endl;
cin>>A1.PI.AD1.house;
cin.getline(A1.PI.AD1.street,30);
cin.getline(A1.PI.AD1.city,30);
cout<<"enter age in years"<<endl;
cin>>A1.PI.age;
cout<<"enter 1 for married and 0 for otherwise"<<endl;
cin>>A1.PI.maritalstatus;
cout<<"enter blood group"<<endl;
cin>>A1.PI.bldgrp;
cout<<"enter M for male and F for female";
cin>>A1.PI.sex;
}
why this happens :
This happens because you have mixed cin and cin.getline.
when you enter a value using cin, cin not only captures the value, it also captures the newline. So when we enter 2, cin actually gets the string ā2\nā. It then extracts the 2 to variable, leaving the newline stuck in the input stream. Then, when getline() goes to read the input, it sees ā\nā is already in the stream, and figures we must have entered an empty string! Definitely not what was intended.
old solution :
A good rule of thumb is that after reading a value with cin, remove the newline from the stream. This can be done using the following :
std::cin.ignore(32767, '\n'); // ignore up to 32767 characters until a \n is removed
A better solution :
use this whenever you use std::getline() to read strings
std::getline(std::cin >> std::ws, input); // ignore any leading whitespace characters
std::ws is a input manipulator which tell std::getline() to ignore any leading whitespace characters
source : learncpp website
goto section (Use std::getline() to input text)
hope this is helpful
#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
don't use .h extension
we don't think we need #include<conio.h> anymore
and isn't simpler
getline(cin, A1.PI.name) than cin.getline(A1.PI.name,50)
then resize its max size
A1.PI.name.resize(50);
Code:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
struct student
{
char name[20];
int id;
float cgpa;
};
int main()
{
student s[100];
int n,i;
cout<<"Enter number of students: ";
cin>>n;
cout<<"Enter records of "<<n<<" students"<<endl;
for(i=0; i<n ; i++)
{
cout<<"Enter name: ";
gets(s[i].name);
cout<<"Enter ID: ";
cin>>s[i].id;
cout<<"Enter CGPA: ";
cin>>s[i].cgpa;
cout<<endl;
}
for(i=0; i<n ; i++)
{
cout<<"\nName: "<<s[i].name;
cout<<"\nID: "<<s[i].id;
cout<<"\nCGPA: "<<s[i].cgpa<<endl;
}
}
output:
Enter number of students: 2
Enter records of 2 students
Enter name: Enter ID:
using sublime text 3
c++
Use <string> for name instead of a character array, then use the cin as usual, instead of gets, to read in the string.
As pointer out by Some Programmer Dude, the enter you pressed is put into the input buffer as a newline '\n' and then later accepted by get() causing it to skip to the next input.
There are two ways you can approach this problem :
Use cin.ignore() at the start of the loop where you take the input.
The better choice will be to get rid of gets(). It has been deprecated since c++14. Secondly, replace the character array with a string.
So the structure would see a modification string name;
And simply do cin>>s[i].name; in the loop.
Thats it.
Also, its preferable to use cstdio instead of stdio.h.
I have the following simple program, but the last line of code getline(cin, topicClass) is never excuted. However, if I use normal cin>>topicClass that is executed. Could you please help me out of this? Thanks
#include <iostream>
#include <string>
using namespace std;
void InfoInput()
{
string classID;
string teacherName;
int totalStudent;
string topicClass;
cout<<"Enter class ID"<<endl;
getline(cin, classID);
cout<<"Enter teacher's name"<<endl;
getline(cin, teacherName);
cout<<"Enter total number of students"<<endl;
cin>>totalStudent;
cout<<"Enter topic of class"<<endl;
getline(cin, topicClass);
//cin>>topicClass;
}
int main ()
{
InfoInput();
}
Your problem is above, with this line:
cin>>totalStudent;
This does not read a line. You enter your input and (I assume) you press ENTER. The \n remains in the buffer of std::cin and is read as an empty line with your next instruction:
getline(cin, topicClass);
To fix, use this code:
cin>>totalStudent;
while(std::isspace(cin.peek())
cin.ignore();
cout<<"Enter topic of class"<<endl;
getline(cin, topicClass);
There is \n remained in cin after reading totalStudent as integer so you need to get that \n first out of the system and then read the next string
#include <iostream>
#include <string>
using namespace std;
void InfoInput()
{
string classID;
string teacherName;
int totalStudent;
string topicClass;
cout<<"Enter class ID"<<endl;
getline(cin, classID);
cout<<"Enter teacher's name"<<endl;
getline(cin, teacherName);
cout<<"Enter total number of students"<<endl;
cin>>totalStudent;
cout<<"Enter topic of class"<<endl;
getline(cin,topicClass);
getline(cin, topicClass);
//cin>>topicClass;
}
int main ()
{
InfoInput();
}
Your classID and teacherName are local variables and disappear after execution leaves the function. You'll have to figure out some way, like passing parameters by reference, to share the variables between functions.
#include<iostream>
using namespace std;
int main()
{
int n=0;
char name[20];
char address[50];
cout<<"Enter a Number";
cin>>n;
cout<<"Enter a Name ="; // HERE
gets(name);
cout<<"Enter a Address=";
gets(address);
cout<<"Number ="<<n<<endl;
cout<<"Name = "<<name<<endl;
cout<<"Address = "<<address<<endl;
}
It has one Integer type and two array of char type..when the program reach at ("Enter a name") why the compiler skip it without showing any problem.
Because when you use cin to get the integer, the newline you press to end the input is left in the input buffer, so when you next get a line the gets function will see this newline and read it as an empty line.
You can ask cin to ignore input until (and including) next newline with
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Here's a slightly modified code :
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
int n=0;
char name[20],c;
char address[50];
cout<<"Enter a Number";
cin>>n;
c = getchar();
cout<<"Enter a Name =";
gets(name);
cout<<"Enter a Address=";
gets(address);
cout<<"Number ="<<n<<endl;
cout<<"Name = "<<name<<endl;
cout<<"Address = "<<address<<endl;
}
Here, I have only performed one modification : taking a character input after the number is entered. This character stores the \n or the Enter you press after typing the input. Earlier, the Name field stored the \n and hence input skipped to the next field, but now we have used a buffer character to store it.