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;
}
Related
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
I'm new to C++.
In a part of a simple project, I have to enter a number and verify that it is positive and not an alphanumeric sequence. With the following code I perform the "check for alphanumeric sequence" but in which way I can add the "positive check"?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string getline()
{
string str;
getline(cin, str);
return str;
}
int main()
{
int choice;
istringstream iss(getline());
iss >> choice >> ws;
if(iss.fail() || !iss.eof())
{
cout << "Error" << endl;
}
}
Thank you in advance!!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
main()
{
bool string1[20];
cout << "Enter string: ";
cin >> string1;
int counter = 0;
int length;
length = strlen(string1);
This is incomplete code, but my question is why am I getting a compiling error when using cin? It says:
error: no match for ‘operator>>’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘bool [20]’)
On this line:
cin >> string1;
I'm not sure how to fix this.
bool string1[20]; is the wrong choice for the user input as a string, all it does is create an array of 20booleans, true or false which is not what you want.
what you are after is your included #include <string>
string string1;
cout << "Enter string: ";
cin >> string1;
Instead of using strlen you get the length by using the length method provided by std::string
auto length = string1.length()
There is no operator>> for reading an array of bool values. What you need is an array of char values instead:
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main() {
char string1[20];
cout << "Enter string: ";
cin >> setw(20) >> string1;
int length = strlen(string1);
Or better, a single std::string:
#include <iostream>
#include <string>
using namespace std;
int main() {
string string1;
cout << "Enter string: ";
cin >> string1;
int length = string1.length();
I think you are confusing string with array. string1 in your code is not an string its an array. So, you can't just put data in it without giving the proper index number. Also remember its an bool type so you can only enter 0/1/true/false value.
Again, you have used strlen() function in your code which is used for determining the length of the string but your is an array. You didn't ask about this but when I ran the code in my IDE it got error.
Here is one way to do it :
main()
{
bool string1[20];
cout << "Enter string: ";
for(int i=0;i<20;i++)//iterating through the boolian array
{
cin >> string1[i];
}
int counter = 0;
int length;
length = sizeof(string1)/sizeof(string1[0]);
cout<<length;//printing the size of the array
}
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