I'd like to use cin and I used char for the int type (do you call it like that?) and it just shows one letter of what typed. How can I get the whole sentence?
Since you're using c++ why not use std::string instead? Something like this should do what you're looking for:
#include <iostream>
#include <string>
int main()
{
using namespace std;
string sentence;
cout << "Enter a sentence -> ";
getline(cin, sentence);
cout << "You entered: " << sentence << endl;
}
use cin.getline()
char name[256];
cout << "What is your name?\n>";
cin.getline(name, 256);
cout << "Your name is " << name;
Related
I have the following C++ code:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <cstring>
using namespace std;
int main() {
istringstream inSS;
string title;
string col1;
string col2;
string val;
int numCommas;
vector<string> stringData();
vector<int> intData();
cout << "Enter a title for the data:" << endl;
getline(cin, title);
cout << "You entered: " << title << endl << endl;
cout << "Enter the column 1 header:" << endl;
getline(cin, col1);
cout << "You entered: " << col1 << endl << endl;
cout << "Enter the column 2 header:" << endl;
getline(cin, col2);
cout << "You entered: " << col2 << endl << endl;
while (1) {
cout << "Enter a data point (-1 to stop input):" << endl;
getline(cin, val);
if(strcmp(val.c_str(), "-1") == 0) {
break;
}
inSS >> stringData >> intData;
cout << "Data string: " << stringData << endl;
cout << "Data integer: " << intData << endl;
}
return 0;
}
Error in question:
main.cpp: In function 'int main()': main.cpp:46:9: error: no match for 'operator>>' (operand types are 'std::istringstream {aka std::cxx11::basic_istringstream<char>}' and 'std::vector<std::cxx11::basic_string<char> >()')
inSS >> stringData >> intData;
~~~^~~~~~~~~~~
What does this error mean? How do I fix it?
The error has to do with a combination of factors. For starters, let's look at this line:
inSS >> stringData >> intData;
Here, you're trying to read from an istringstream into a vector<string> and vector<int>. However, you can't use the stream extraction operator to read a vector from a stream - there's no fundamental reason why not, it's just that the standard doesn't allow it. You'll need to read that data one element at a time, which will likely require you to rewrite a lot of this code.
There's another more subtle issue here. These lines don't do what you think they do:
vector<string> stringData();
vector<int> intData();
These lines look like they declare variables named stringData and intData of type vector<string> and vector<int>, using the default constructors. Unfortunately, C++ interprets these, believe it or not, as function prototypes. That first one is a prototype of a function named stringData that takes no arguments (hence the emptiness between the parentheses) and returns a vector<string>, for example. To fix this, drop the parentheses. Just write
vector<string> stringData;
vector<int> intData;
To summarize:
You'll need to fundamentally make some changes to your code, since you can't read from an istringstream into a vector. That necessitates a logic update.
You'll need to fix the two declarations from earlier on by dropping the parentheses.
I have just started C++ after working with C for almost a year. I'm writing a program for a user to input info about a song. I read that I should use getline() to read strings with spaces. Here is my code:
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
typedef struct Song
{
char title[20];
char album[20];
char artist[20];
int year;
} song;
//store song info
Song Input;
char inputStr[20];
int inputYear;
cout << "Enter the name of a song: ";
getline(cin, inputStr);
strcpy(Input.title, inputStr);
cout << "Enter the album of your song: ";
getline(cin, inputStr);
strcpy(Input.album, inputStr);
cout << "Enter the artist of your song: ";
getline(cin, inputStr);
strcpy(Input.artist, inputStr);
cout << "Enter the year your song was released: ";
cin >> inputYear;
Input.year = inputYear;
//print
cout << "Song title: " << Input.title << endl;
cout << "From album: " << Input.album << endl;
cout << "Artist: " << Input.artist << endl;
cout << "Released: " << Input.year << endl;
return 0;
}
My compiler1 throws 3 errors, one for each of the getline() calls, not recognizing getline() despite the fact I have #include <string>. I have looked up sample usage of the getline() function.
Thanks for any help.
1I have wondered if this problem might concern an issue with the standard of C++ that my compiler supports. I did a bit of research and I did not find anything that helped me learn which standard I am using. Here's some info:
I'm using Terminal on Mac.
After g++ version:
Configured with: --prefix=/Applications/Xcode.app/Cont.../usr/include/c++/4.2.1
Apple LLVM version 8.1.0 (clang-802.0.42)
These lines seem to be the only ones of use here, but I could give more info. If someone has any idea which standard of C++ this is, whether it's C++11, or C++14, or otherwise, that would also be very helpful. Thanks again.
UPDATE:
started from scratch, tried to take as much of your advice as possible while still sticking to some of what I know. No errors and works just as I hoped. Thanks for all your help.
New code:
#include <string>
#include <cstring>
#include <iostream>
using namespace std;
struct Song
{
string title;
string artist;
string album;
string year;
}song;
int main()
{
Song Input;
cout << "Song? ";
getline(cin, Input.title);
cout << "Artist? ";
getline(cin, Input.artist);
cout << "Album? ";
getline(cin, Input.album);
cout << "Year? ";
getline(cin, Input.year);
cout << "Song: " << Input.title << endl;
cout << "Artist: " << Input.artist << endl;
cout << "Album: " << Input.album << endl;
cout << "Year: " << Input.year << endl;
return 0;
}
The version of getline you are using takes a std::string as a parameter, not an array of char. If you want to use an array of char (and you shouldn't), you need to use the member function version:
cin.getline( some_char_array, array_size );
I would switch from using char arrays to using string's everywhere. For example I would do your code like this:
#include <string>
#include <iostream>
struct Song{
std::string title;
std::string album;
std::string artist;
int year;
};
int main()
{
//store song info
Song Input;
int inputYear;
std::cout << "Enter the name of a song: ";
getline(std::cin, Input.title);
std::cout << "Enter the album of your song: ";
getline(std::cin, Input.album);
std::cout << "Enter the artist of your song: ";
getline(std::cin, Input.artist);
std::cout << "Enter the year your song was released: ";
std::cin >> Input.year;
//print
std::cout << "Song title: " << Input.title << '\n';
std::cout << "From album: " << Input.album << '\n';
std::cout << "Artist: " << Input.artist << '\n';
std::cout << "Released: " << Input.year << std::endl;
return 0;
}
My preference is to not use using namespace std; but there's nothing wrong with it. Notice that using strings directly I don't need to copy things. I can use getline to do all that for me. I also don't need to worry about overrunning the size of the char array because string does that for me as well.
I am wringing a simple code to learn more about string. When I ran my code it would not print my last name. Can someone explain why? I used string phrase to store it and it only appears to have stored my first name. Here is the code.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting;
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
cin >> phrase;
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
I used string phrase to store it and it only appears to have stored my first name.
That makes sense.
cin >> phrase;
will stop reading when it encounters a whitespace character in the input.
To read the full name you can use one of the following approaches.
Use two calls to cin >>.
std::string first_name;
std::string last_name;
cin >> first_name >> last_name;
Use getline to read the entire line. getline will read everything in a a line, including whitespace characters.
getline(cin, phrase);
When you call cin >> phrase;, it only reads the string up to the first non-space character. If you want to include spaces in your name, best goes with getline(cin,phrase);.
IMPORTANT: getline() will reads whatever it is in the stream buffer up to the first \n. It means that when you enter cin >> greeting;, if you hit ENTER, getline() will read everything before that \n that is not already read, which is NOTHING into your phrase variable, making it an empty string. An easy way out is to call getline() twice. E.g.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting; //IMPORTANT: THIS ASSUME THAT GREETING IS A SINGLE WORD (NO SPACES)
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
string rubbish_to_be_ignored;
getline(cin,rubbish_to_be_ignored); //this is going to read nothing
getline(cin, phrase); // read the actual name (first name and all)
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
Assuming you store that code in the file stackoverflow.cpp. Sample run:
Chip Chip#04:26:00:~ >>> g++ stackoverflow.cpp -o a.out
Chip Chip#04:26:33:~ >>> ./a.out
Exercise 3B
Kaitlin Stevers
String arrays
Please enter a greeting:
Hello
The greeting you entered was: Hello
Enter your full name
Kaitlin Stevers
Hello, how are you today Kaitlin Stevers?
Tested on ubuntu 14.04
I'm new to C++ and I decided to create a small script to get used to the language.
All the script does is that it asks a question to a user, a user then types in a string, and then the script returns with an output with a scripted message followed by the user's input.
My problem is that when a user types in name of more than one word the script will only return the first word, e.g
What is your name?
Donald Duck
Welcome Donald
Below is my script:
int main(int nNumberofArgs, char* pszArgs[])
{
string name;
cout << "What is your name?\n";
cin >> name;
cout << "Welcome " << name;
cout << "\nThis is your homepage. Enjoy your stay!" << endl;
system("PAUSE");
return 0;
}
Just use std::getline:
#include <string>
#include <iostream>
int main()
{
std::string name;
std::cout << "What is your name?\n";
std::getline(std::cin, name);
std::cout << "Welcome " << name << std::endl;
}
I have been working on this program for a while and I finally got rid of the compile errors. But when I tried it, the program basically skipped a line of code.
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(){
string nameOfFile = "";
char index;
char title[100];
char name[100];
char copyright[100];
cout << "Welcome, and hello to the html templating software" << endl;
cout << "Is this your index page?\ny/n" << endl;
cin >> index;
if (index=='n'){
cout << "Enter the prefered name of this file" << endl;
getline(cin, nameOfFile, '\n');
}
cout << "What will the title of the page be?" << endl;
cin.getline(title, 100);
cout << "What is your name?" << endl;
cin.getline(name, 100);
cout << "What is the copyright?" << endl;
cin.getline(copyright, 100);
cin.get();
return 0;
}
You see how after asking if this is your index page it skips the next cin.getline function no matter the scenario.
When the user entered the index, they also typed a newline, but your cin didn't remove it from the input stream. So, your call to cin.getline returns immediately because of the leftover newline.
Add a call to cin.ignore before the cin.getline to flush it out.
replace getline(cin, nameOfFile, '\n')
with
cin >> nameOfFile