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
Related
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 new to c++ but do have a basic knowledge in coding. This program works fine and well but I'm wondering if there is a better way to do this.
The program makes a star wars name by taking the first three letters of your last name and the first 2 of your first name to make your first name of your star wars name. Then for your star wars surname it takes the first two letters of your mother's maiden name and the first three letters of the city you were born in.
// starWarsName.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
string firstName;
string surname;
string maidenName;
string city;
cout << "This program is designed to make you a star wars name, it takes some information and concatinates parts of the information to make your NEW name" <<endl << endl;
cout << "please enter your first name" << endl;
cin >> firstName;
cout << "please enter your surname" <<endl;
cin >> surname;
cout << "what is your mothers maiden name?" << endl;
cin >> maidenName;
cout << "please tel me which city you were born in" << endl;
cin >> city;
cout << firstName << " " << surname << endl;
cout << firstName[0] << " " << surname << endl;
int size = firstName.length();
//cout << size;
cout << surname[0] << surname[1] << surname[2] << firstName[0] << firstName[1];
cout << " " << maidenName[0] << maidenName[1] << city[0] << city[1] << city[2];
cin.get();
cin.ignore();
return 0;
}
You can use string::substr here to store character sequence instead of writing surname[0]..surname[2] again and again.
Here is an example of string::substr
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}
Output:
think live in details.
Okay, I am trying to use the code:
getline(cin, phrase);
When I compile I get the error:
no matching function for call to 'getline'
Here is the full code:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
cout << "Challenge 1\n" << "Kaitlin Stevers\n" << "Characters and Strings" << endl;
cout << endl;
cout << endl;
char letter[2];
cout << "Please enter a letter: " << endl;
cin >> letter;
cout << "You entered: " << letter << endl;
char word[5];
cout << "Please enter a word up to 5 characters long: " << endl;
cin >> word;
cout << "The word you entered is: " << word << endl;
char phrase[100];
cout << "Please enter a phrase up to 99 characters long: " << endl;
getline(cin, phrase);
cout << "The phrase you entered is: " << phrase << endl;
string lettero;
cout << "Enter one letter: " << endl;
cin >> lettero;
cout << "The letter you entered is: " << lettero << endl;
string wordo;
cout << "Please enter a word: " << endl;
cin >> wordo;
cout << "The word you entered is: " << wordo << endl;
string phraseo;
cout << "Please enter five words: " << endl;
getline(cin, phraseo);
cout << "The words you entered are: " << phraseo << endl;
return 0;
}
'no matching function call for getline', cause getline takes a string not a char[] as argument. See cin.getline() if you absolutely want to pass a cha[] as argument.
As you see here.
This getline(cin, string) function accepts a string.
Although, there is also an instruction you can use to put the line into a char array like so:
char phrase[99];
cin.getline (phrase,99);
Or you could also get the input into a string, then convert it to a char array :
string temp = "";
cin >> temp;
char phrase[99];
strcpy(phrase, temp.c_str());
I am trying to read in a sentence to unscramble, however something is going wrong. When I enter no character it'll print out "The sentence is" and "Decoded sentence is", but when I enter one or more characters it'll just sit there and do nothing. I don't think it could be an error with MySentence class because it does not even print "The sentence is".
#include <iostream>
#include <stdio.h>
#include "MySentence.h"
#include "Corpus.h"
#include <string>
using namespace std;
int main() {
Corpus corp;
std::cout << "The proportions are: ";
for(int i = 0; i<26; i++) {
cout << corp.proportion(i+97) <<", ";
}
cout << endl;
cout << "Enter sentence terminated by <ENTER> ";
string s= "";
getline(cin, s);
cout << "The sentence is " << s;
MySentence sent(s);
sent.decode(corp);
cout << endl << "Deoded sentence is: " << sent.sentence;
return 0;
}
As suggested by n.m. try to add an endl at the end of the line
cout << "The sentence is " << s << endl;
since it is possible that the buffer is not being flushed and the problem is in the class MySentence.
An interesting post that might help would be
Buffer flushing: "\n" vs. std::endl
I am trying to learn c++. I am on strings now.
I have written this simple method that should ask to input a string and then return it. To do so I am using cin.getLine() method but the string is not printed after I use cin.getLine()
string getString(char string[])
{
cout << "Please enter a string to process ";
cin >> string;
cout << "String in getString before process: " << string << "\n";
cin.getline(string, STRINGSIZE);
cout << "String after processing: " << string << "\n"; // here string is not printed
return string;
}
Can anybody help me to understand what I am doing wrong? Thank you
you are first reading string to std::string with cin >> string; and then read again something from cin with cin.getline(string, STREAMSIZE);
it is not necessary, read it once and return:
string getString(char string[]){
cout << "Please enter a string to process ";
cin >> string;
cout << "String in getString before process: " << string << "\n";
// process this, do whatever you describe as processing it
cout << "String after processing: " << string << "\n"; // string is printed
return string;
}
otherwise, if you want to use getline, do:
std::string name;
std::cout << "Please, enter your full name: ";
std::getline (std::cin,name); // or std::getline(std::cin,string, 'r'); to read
//only to delimiter character 'r'
std::cout << "Hello, " << name << "!\n";
so thing to remember is use getline OR cin, not both simultaneously unless there is really some special reason