I want to separate a full name from each other. It only works for first name.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string FullName;
int i = 0;
cout <<"Enter your full name "<<endl;
getline(cin,FullName);
while (FullName[i] != ' ')
{
cout<<FullName.substr(i,FullName.find(' '))<<endl;;
i++;
}
cout <<endl;
}
return 0
}
I want to separate each name in a separate line like this: If I enter this:
Max Michael Max
the output should be with each name in a separate new line:
Max
Michael
Max
How can I split names each one in separate line ?
The simplest approach is to use std::istringstream after the name is read in.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string FullName;
cout <<"Enter your full name "<<endl;
getline(cin, FullName);
string namepart;
istringstream strm(FullName);
while ( strm >> namepart )
cout << namepart << '\n';
}
Live Example
Related
The code is meant to count the number of times of the specific word you type from a text file.
My code so far:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string word;
string wif;
fstream file("words.txt");
int nTimes = 0;
cin >> word;
while (file >> wif) {
cout << wif << " ";
if (wif == word) {
++nTimes;
}
}
cout << nTimes << endl;
return 0;
}
The code works but not what i expect.
For an example: the file has a total of 6 "many" words, but the output is 4, some of the words are like this: Many, MANY?
Is it not adding those words because of the characters , and ?
I want the user to enter the name of a file, and if the file exists, print out all the contents of the file.
At the moment the uncommented code, takes a name of a file that the user inputs, for example. example.txt and prints out most (not the last word?) of the file. I've tried to implement this instead by using string (commented code is attempt) but clearly its incorrect.
I also wondering if i can automatically add .txt to the end of the user input, so that the console could ask - "which subject should we find more information on" user inputs "math" and it will open "math.txt"
Here is what I´ve tried:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
char filename[50];
//string getcontent;
ifstream name;
cin.getline(filename, 50);
name.open(filename);
if (!name.is_open()) {
exit(EXIT_FAILURE);
}
char word[50];
name >> word;
while (name.good()) {
cout << word << " ";
name >> word;
}
//if (!name.is_open()) {
//while (! filename).eof())
//{
//getline(name, getcontent)
//cout << getcontent << endl;
//}
//exit(EXIT_FAILURE); //comes from cstdlib
//}
//}
system("pause");
return 0;
}
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int main() {
string filename;
string getcontent;
ifstream name;
cin >> filename;
filename.append(".txt"); // add extension.
name.open(filename);
if (!name.is_open()) {
exit(EXIT_FAILURE);
}
while (true)
{
getline(name, getcontent);
if (name.eof()) break;
cout << getcontent << endl;
}
return 0;
}
I found this and it helped me with a somewhat different problem and I also thought that I might be able to help. This is coded in windows. (I'm a beginner so forgive me if I made some obvious mistakes)
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin;
int main()
{
//char filename[50],word[50];
string filename,word;
//cin.getline(filename,50);
getline(cin,filename);
//strcat(filename,".txt");
filename.append(".txt");
fin.open(filename);
if(fin.is_open())
while(fin>>word)
cout<<word<<endl;
else
cout<<"No such file"<<endl;
return 0;
}
I'm having trouble with splitting a string with sstream. It appears when the loop of cout happens, the last item of data, an integer in my case, is repeated? Is my loop set up wrong??
This is my code:
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
string inputText("Jane Smith 18");
istringstream iss(inputText);
while (iss)
{
string first;
string last;
int age;
iss >> first >> last >> age;
cout << first << " " << last <<" "<< age << endl;
}
system("pause");
return 0;
}
See the picture - showing how the 'age' variable seems to be repeated. What am I doing wrong?
Thanks for your help in advance!
Console-Example
So I am trying to get the user to input their name and height.
I have got other code.
I have got this.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int name1;
cout << "What's your name?";
cin >> name1;
int height1;
cout << "What's your height?";
cin >> height1;
return 0;
}
The issue is that it won't allow the user to enter their height. Any ideas?
The Problem is, that you're using int variables instead of std::string. However, you already included the <string> header file, so you probably want to do this:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
std::string name1;
cout << "What's your name?";
cin >> name1;
std::string height1;
cout << "What's your height?";
cin >> height1;
return 0;
}
Otherwise it only works if you enter integer numbers - but that doesn't make much sense for an 'name' input.
Edit: If it's also your requirement to input names with whitespaces, you could use std::getline
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
std::string name1;
cout << "What's your name?";
getline(cin, name1);
std::string height1;
cout << "What's your height?";
getline(cin, height1);
return 0;
}
Am I using the getword function wrong here? The compiler keeps telling me that there is no member function.
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
using namespace std;
int OccuranceOfString(ofstream & Out)
{
string Occur;
string Temp;
int OccurLength;
int count;
cout << "please enter to string to search for";
cout << endl;
cin >> Occur;
OccurLength = Occur.length();
while(Out.getword(Temp))
{
if (Temp == Occur)
{
count ++;
}
}
return count;
}
Whats wrong with my code? I'm trying to find all occurances of a string with this function
std::ofstream has no getword function: see here.
Perhaps you're thinking of std::getline.
There is no function getword in the header files listed. You simply must construct a function that will extract words from a line. capture a line by
getline(out,line);
line will have your line of string and use line[index] to get continuous characters to be equal to a word.
You can use this
std::string::find
do something like this..
int pos = 0;
int occurrences = 0
string input = "YAaaaAH";
string find = "a";
while(pos != -1){
pos = input.find(find,pos);
occurrences++;
}
text file :
code :
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream file("DB_name_age.txt");
int index;
string name;
int age;
if(file.is_open())
{
while(file >>index >> name >>age)
{
cout << index <<" "<<name <<" "<<age << endl;
}
}else{
cout<< "file open fail" <<endl;
}
return 0;
}
visual explanation: