Why isn't my string splitting like it should? - c++

I'm working on a project of splicing an inputted string of a name, and for some reason it's not working. Part of it is code copied from my book that supposedly works, so I'm stuck. Am I doing something wrong?
#include <iostream>
#include <string>
using namespace std;
void main()
{
string name;
int index;
cout<<"Please enter your full name. ";
cin>>name;
cout<<"\n"<<endl;
index = name.find(' ');
cout<<"First Name: "<<name.substr(0, index)<<" "<<name.substr(0, index).length()<<endl;
name = name.substr(index+1, name.length()-1);
index = name.find(' ');
cout<<"Middle Name: "<<name.substr(0, index)<<" "<<name.substr(0, index).length()<<endl;
name = name.substr(index+1, name.length()-1);
cout<<"Last Name: "<<name<<" "<<name.length()<<endl;
}

Most peoples' names consist of at least two words. This will only get one of them:
cout<<"Please enter your full name. ";
cin>>name;
istream operator>> is whitespace delimited. Use getline instead:
std::getline(std::cin, name);
For your purposes, you could probably do this, which is simpler:
std::string first, middle, last;
std::cin >> first >> middle >> last;

Related

How do I get the first x characters of a string to be measured and printed out?

I've been assigned a problem at school and it goes as follows:
Write a program that creates a login name for a user, given the user's
first name, last name, and a four-digit integer as input. Output the
login name, which is made up of the first five letters of the last
name, followed by the first letter of the first name, and then the
last two digits of the number (use the % operator). If the last name
has less than five letters, then use all letters of the last name.
The sample output is:
Enter first name: John
Enter last name: Doe
Enter last 4 digits of social security number: 8457
Your login name: DoeJ57
So far this is my code:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
string firstName;
string lastName;
int ssn;
int lastTwo;
char firstCharacter;
string lastNameLength;
cout << "Enter first name:";
cin >> firstName;
cout << "Enter last name:";
cin >> lastName;
cout << "Enter last 4 digits of social security number:";
cin >> ssn;
firstCharacter = firstName.front();
lastTwo = (ssn % 100);
lastNameLength = lastName.length();
cout << lastNameLength;
return 0;
}
I tried measuring the length of the last name for it to print out but now I'm stuck on how to get the last name sized and then printed, any guidance is appreciated.
std::string has a substr() method for extracting a portion of the string. If you ask it for the 1st 5 characters, and the string is shorter than 5 characters, then the entire string will be returned. Which is exactly what your instructions ask for.
Try something more like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string firstName;
string lastName;
int ssn;
cout << "Enter first name:";
cin >> firstName;
cout << "Enter last name:";
cin >> lastName;
cout << "Enter last 4 digits of social security number:";
cin >> ssn;
cout << lastName.substr(0, 5) << firstName.front() << ssn % 100;
return 0;
}
Online Demo

Writing 2 names (with spaces) in C++ (cin command)

I am beginner to the C++ and was trying to build first codes. Right now I'm trying to learn cin command but there is a problem. When I use this code:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
string surname;
cout<<"Please enter your name: ";
cin>>name;
cout<<"Please enter your surname: ";
cin>>surname;
cout<<"Your name: "<<name <<endl;
cout<<"Your surname: "<<surname;
}
The output is:
Please enter your name: aaa bbb
Please enter your surname: Your name: aaa
Your surname: bbb
So when I use 2 names in the 'name' variable, this happens. But when I just write 'aaa' in the name and 'bbb' in the surname, it works perfectly fine:
Please enter your name: aaa
Please enter your surname: bbb
Your name: aaa
Your surname: bbb
What do I have to do? Thanks.
Use the getline function:
#include <iostream>
#include <string>
using namespace std;
int main() {
string name, surname;
cout << "Please enter your name: ";
getline(cin, name);
cout << "Please enter your surname: ";
getline(cin, surname);
cout << "Your name: " << name << endl;
cout << "Your surname: " << surname;
}
cin reads just till the first space, but getline reads till the first new line. That means that cin only takes the first word, and the next word "rests" unpicked on the console. When the next cin comes, it takes that "resting" word.
You have to include the #include<string> header when using function getline. You specify the input type as first argument (that's what you put in the parentheses – in our case cin), and as second argument is the string where you want to save that value. For example, getline(cin, input);. If you don't use namespace std;, you must also specify the namespace, so like this: std::getline(std::cin, input).
If you are still learning C++, you won't understand this, but maybe a little later: there are actually two forms ("overloads") of getline function: one is like I said before (istream& getline (istream& is, string& str, char delim);), but another also uses the 3rd parameter (istream& getline (istream& is, string& str);), like this: getline(cin, input, ';');. With that code, the input stream ends the input at the first semicolon. Note the ' sign, not " sign!

naming the file in c++

when i run this code the 2nd & 3rd files' names the first character disappear
for example if i named the 2nd file set2.txt it will be et2.txt
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream set1,set2,set3;
string name;
cout<<"Enter set1 name: ";
cin.ignore();
getline(cin , name);
set1.open(name,ios::out);
cout<<"Enter set2 name: ";
cin.ignore();
getline(cin , name);
set2.open(name,ios::out);
cout<<"Enter set3 name: ";
cin.ignore();
getline(cin , name);
set3.open(name,ios::out);
}
the first character disappear
That is because you ignore the first character with the line
cin.ignore();
before you read the a line of text with
getline(cin , name);
These two lines are in the wrong order. Instead they should be
getline(cin , name);
cin.ignore();
The purpose of cin.ignore() here is to remove the newline character from the input stream after you read a line of text with getline().

getline (cin, string) is not excuted last line of code

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.

Find and Modify txt File in C++

I want to find a specific id from file and modify content.
Here is originol code which I want.
// test.txt
id_1
arfan
haider
id_2
saleem
haider
id_3
someone
otherone
C++ Code:
#include <iostream>
#include <fstream>
#include <string>
using namesapce std;
int main(){
istream readFile("test.txt");
string readout;
string search;
string Fname;
string Lname;
cout<<"Enter id which you want Modify";
cin>>search;
while(getline(readFile,readout)){
if(readout == search){
/*
id remain same (does not change)
But First name and Last name replace with
user Fname and Lname
*/
cout<<"Enter new First name";
cin>>Fname;
cout<<"Enter Last name";
cin>>Lname;
}
}
}
Suppose:
A user search id *id_2*. After that user enter First name and Last name Shafiq and Ahmed.
After runing this code the test.txt File must modify the record like that:
...............
...............
id_2
Shafiq
Ahmad
.................
.................
Only id_2 record change remaing file will be same.
UPDATE:
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
int main()
{
ofstream outFile("temp.txt");
ifstream readFile("test.txt");
string readLine;
string search;
string firstName;
string lastName;
cout<<"Enter The Id :: ";
cin>>search;
while(getline(readFile,readLine))
{
if(readLine == search)
{
outFile<<readLine;
outFile<<endl;
cout<<"Enter New First Name :: ";
cin>>firstName;
cout<<"Enter New Last Name :: ";
cin>>lastName;
outFile<<firstName<<endl;
outFile<<lastName<<endl;
}else{
outFile<<readLine<<endl;
}
}
}
It also contain pervious First Name and Last Name in temp.txt file.
After finding the specific id and writing the new first name and last name, you need to skip the following two lines. This code works:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void skipLines(ifstream& stream, int nLines)
{
string dummyLine;
for(int i = 0; i < nLines; ++i)
getline(stream, dummyLine);
}
int main()
{
ofstream outFile("temp.txt");
ifstream readFile("test.txt");
string readLine;
string search;
string firstName;
string lastName;
cout<<"Enter The Id :: ";
cin>>search;
while(getline(readFile,readLine))
{
if(readLine == search)
{
outFile<<readLine;
outFile<<endl;
cout<<"Enter New First Name :: ";
cin>>firstName;
cout<<"Enter New Last Name :: ";
cin>>lastName;
outFile<<firstName<<endl;
outFile<<lastName<<endl;
skipLines(readFile, 2);
}
else
{
outFile<<readLine<<endl;
}
}
}