What is causing this error about string? - c++

#include <iostream>
#include <string>
int main(void)
{
using std::cout;
using std::cin;
using std::string;
string name;
int n1, n2;
cout << "What is your name ?\n";
cin >> name;
cout << "Hello " << name.c_str() <<"!\n"
<< "Please give me two number separated by space\n";
cin >> n1 >> n2;
cout << "Sum of " << n1 << " + " << n2 << " is " << n1 + n2 << "\n";
return 0;
}
My console input/output looks like this:
What is your name ?
John Titor
Hello John!
Please give me two number separated by space
Sum of 0 + 1961462997 is 1961462997
It doesn't print the full name, only "John", and it doesn't even ask me about puting two numbers.

You should use std::getline to get a string with spaces. std::cin separates the strings by spaces.
getline(cin, name);
In addition, you can print a std::string by std::cout without .c_str():
cout << name;

Related

cquery no matching funtion for call 'to_upper'

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
using namespace std;
int main() {
string city1, city2;
cout << ("Please enter your citys name");
cin >> city1;
cout << ("Please enter your citys second name");
cin >> city2;
cout << city1 [0,1,2,3,4];
cout << city2 [0,1,2,3,4];
boost::to_upper(city1, city2);
cout << city1,city2;
}
This is my code and for some reason boost::to_upper(city1, city2); gets the error: [cquery] no matching funtion for call 'to_upper'
boost::algorithm::to_upper is declared as (from boost reference)
template<typename WritableRangeT>
void to_upper(WritableRangeT & Input, const std::locale & Loc = std::locale());
so you can pass only one string to this function. Replacing
boost::to_upper(city1, city2);
with
boost::to_upper(city1);
boost::to_upper(city2);
makes the code compile and an example output is Please enter your citys namePlease enter your citys second nameosLONDON.
It lacks newline characters and there is one more mistake - misunderstanding of comma operator. Generally comma is used to separate arguments or array elements but in lines
cout << city1 [0,1,2,3,4];
cout << city2 [0,1,2,3,4];
// ...
cout << city1,city2;
comma operator is used. Comma operators takes two operands and it's value is value of the right operand (e.g. after int x = (1, 2); variable x is equal to 2). Code above is equivalent to
cout << city1[4];
cout << city2[4];
// ...
cout << city1;
city2;
Finally, the corrected code is
#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>
using namespace std;
int main() {
string city1, city2;
cout << "Please enter your citys name" << std::endl;
cin >> city1;
cout << "Please enter your citys second name" << std::endl;
cin >> city2;
cout << city1 << std::endl;
cout << city2 << std::endl;
boost::to_upper(city1);
boost::to_upper(city2);
cout << city1 << std::endl << city2 << std::endl;
}

C++ Code not displaying the output in the same line [duplicate]

This question already has answers here:
Removing trailing newline character from fgets() input
(14 answers)
Closed 5 years ago.
#include<iostream>
using namespace std;
int main()
{
char str[10] = "Anmol" ;
int age = 17 ;
cout << "Enter your name here :- " ;
fgets(str, sizeof(str), stdin) ;
cout << "Enter your age here :- " ;
cin >> age ;
cout << "Hello World, It's " << str << "And my age is " << age ;
return 0 ;
}
On running the code, the compiler is giving output in different line like:-
fgets() is a file function which is used to read text from keyboard, as in “file get string.”
fgets() function is read the string as well as "enter" character ascii code which is 13 (carriage return - CR) .so the above code consider the CR character at the end of the 'str' that's why it print in the next line .
You can use the gets_s() function to take the string from the keyboard.
Try the below code .
#include<iostream>
using namespace std;
int main()
{
char str[10] = "Anmol";
int age = 17;
cout << "Enter your name here :- ";
gets_s(str);
cout << "Enter your age here :- ";
cin >> age;
cout << "Hello World, It's " << str << " And my age is " << age;
return 0;
}
try replace '\r\n', and '\n\r' with '' in str
look at here for replace in string : How to replace all occurrences of a character in string?
Try this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int age;
cout << "Enter your name here :- " ;
cin >> str;
cout << "Enter your age here :- " ;
cin >> age ;
cout << "Hello World, It's " << str
<< " And my age is " << age << endl;
return 0 ;
}
When you use fgets(), you also get the ending newline character in the input. That explains your output. You could use std::getline to avoid that problem.
int main()
{
std::string str = "Anmol" ;
int age = 17 ;
cout << "Enter your name here :- " ;
std::getline((std::cin, str) ;
cout << "Enter your age here :- " ;
cin >> age ;
cout << "Hello World, It's " << str << " and my age is " << age << std::endl;
return 0 ;
}

Getline not recognized by compiler

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.

is there a better way? new to c++

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.

I need help associating valus to strings in order to calculate an average

I need help associating values to certain strings in order to calculate an average after an user inputs them.
So I tried using string::find since the values are sequential;
And I ended up with this code:
string str1, str2, str3, str4, str5;
string::size_type position;
float media;
str1 = "NSMBE";
cout << str1.find("N") +1 << endl;
cout << str1.find("M") + 1 << endl;
cout << str1.find("M") + 1 << endl;
cout << str1.find("B") + 1 << endl;
cout << str1.find("E") + 1 << endl;
But now how I go from here to get the user input and making the strings equal to each value?
Example: User inputs an N for grade1 and a M for grade2; so N=1 and M=2 , so the average I should get is (1+2)/2. ..
I'm sure it's really trivial and easy to do, but I searched everywhere in order to find a answer and couldn't.
If you are trying to associate a string (or char) with a value, you should use an associative container such as std::map. I am not sure if I understood your question properly but here is something that might get you started.
#include <iostream>
#include <map>
using namespace std;
int main(int argc, char *argv[]) {
string str1, str2, str3, str4, str5;
str1 = "NSMBE";
map<char, int> values;
cout << "Enter a letter: ";
for (char key; cin >> key; cout << "Enter a letter: ")
{
int val;
cout << "enter a value: ";
cin >> val;
values[key] = val;
}
int sum = 0;
for (char c : str1)
{
cout << "key = " << c << endl;
sum += values[c];
}
cout << endl;
cout << sum << endl;
cout << "Value of the string: " << double(sum)/double(str1.size()) << endl;
}