Taking in input with spaces using std::getline() in c++ - c++

I am pretty new to coding in c++, and I am using Visual Studio Code on MAC. I have tried to find the right code so that I can enter a string with spaces, but everything that I tried hasn't worked. I tried std::getline(std::cin, name), but when I ran the code, and it came time to input the name, it just showed nothing where the name should have been. I don't know if there is something wrong with Visual Studio Code, or if there is just a little bit of code that I am missing. Here is the code that I am having a problem with.
#include <iostream>
#include <string>
#include <cmath>
#include <sstream>
#include <string.h>
int main (){
std::string name;
std::cout << "Please, enter the name of your nation: ";
std::getline(std::cin, name);
std::cout << "General Williams: " << name << " is a great name!\n";
}

I executed your code and it worked.
Maybe you can not see your result because you run with .exe!
please Add "std::getchar()" to end of your code and try it again.
int main()
{
std::string name;
std::cout << "Please, enter the name of your nation: ";
std::getline(std::cin, name);
std::cout << "General Williams: " << name << " is a great name!\n";
std::getchar();
}

Related

VS code c++ cin not working with string type

This is a simple hello program, but it shows nothing if I run it.
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Please enter your name: ";
string username;
cin >> username;
cout << "hello, " << username << ", ..., goodbye\n";
}
output:
nothing in the terminal
The single cout can work, so I guess that might be the problem with buffer caused by string.
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Please enter your name: ";
//string username;
//cin >> username;
//cout << "hello, " << username << ", ..., goodbye\n";
}
output:
Please enter your name:
But adding flush doesn't make it work.
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Please enter your name: ";
string username;
cin >> username;
cout << "hello, " << username << ", ..., goodbye\n" << flush;
}
output: nothing in the terminal
Then I change the type of username to char, int, and they all work properly.
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Please enter your name: ";
char username;
cin >> username;
cout << "hello, " << username << ", ..., goodbye\n";
cout << "Please enter your name: ";
int username;
cin >> username;
cout << "hello, " << username << ", ..., goodbye\n";
}
output:
Please enter your name: asdf
hello, a, ..., goodbye
Please enter your name: 123
hello, 123, ..., goodbye
The above are run by code-runner, so I try to compile manually, but it still doesn't work.
DINO#DINO-Amadeus MINGW64 /d/DINO/code/essential_c++
$ g++ -o chap1 chap1.cc
DINO#DINO-Amadeus MINGW64 /d/DINO/code/essential_c++
$ ./chap1
DINO#DINO-Amadeus MINGW64 /d/DINO/code/essential_c++
$
Can anyone help with this? Many thanks.
I am able to compile your first example,
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Enter name" << endl;
string username;
cin >> username;
cout << "Hello, " << username << ", goodbye\n";
}
and have it work in Codeblocks' console as expected.
Enter name
John Doe
Hello, John, goodbye
Process returned 0 (0x0) execution time : 3.623 s
Press any key to continue.
It also compiles manually through cygwin.
*****#DESKTOP-******* /cygdrive/c/Users/*****/Documents/Programs/Test/test
$ g++ main.cpp -o main
*****#DESKTOP-******* /cygdrive/c/Users/*****/Documents/Programs/Test/test
$ ./main
Enter name
John Doe
Hello, John, goodbye
All this makes me think the code is fine, and the real problem is elsewhere. You said you are using coderunner. Is this for a class? You might be able to see what other students are doing different.
EDIT 10/9/2021
Lets try the following to see what is messing up:
Whether the problem is with the namespace, see if the below code works: (If you type "Hello", it will print "Hello")
#include <iostream>
#include <string>
int main() {
std::string username;
std::cin >> username;
std::cout << username;
}
To see if the problem is with cin, try this (It should output "Hello")
#include <iostream>
#include <string>
int main() {
std::string username = "Hello";
std::cout << username;
}
And, as a moonshot: (Same behavior as (1))
#include <iostream>
int main() {
char* username[5];
scanf("%s",&username);
printf("%s",username);
}
Try changing the extension of VSCode which runs your code, and update to the latest version if you can. If you are using .run, try C/C++ Compile Run by danielpinto8zz6 or similar.

C++ "Reference to overloaded function" error using namespace std

I am new to C++, and I keep getting an error saying:
Reference to overloaded function could not be resolved; did you mean to call it?
Below is the code that is causing this:
#include <stdio.h>
#include <string>
using namespace std;
int main() {
string name;
string age;
cout << "Enter your name and age: ";
cin >> name >> age;
cout << "Hello, " << name << ", are you " << age << " years old?\n";
return 0;
}
I am using Xcode on Mac OS X Mojave.
I also noticed that if I have only that code, then it works just fine, but when I have multiple files, all of them fail to work.
Can anyone tell me why this is and a solution for it?
stdio.h does not define std::cin and std::cout. That header defines the C functions for input and output, like printf and scanf. So it is an I/O header, but it's not the one you need.
You need to include <iostream> to have std::cin and std::cout

Cin and Cout not working

#include <iostream>
#include "stdafx.h"
#include <string>
using namespace std;
int main()
{
//Variables in which student info is stored
string name = "";
//asks user for their name
cout << "/nHello, my name is Hal, what is your name? ";
cin >> name;
cout << "/nWelcome to C++ ";
cout << name;
cout << ", let the adventure begin!";
return 0;
}
Really basic code that I can't seem to get to work. Everywhere I have cin or cout the compiler says they're an undeclared identifier. I've looked at all of the answers on here and other forums and it none of the solutions seem to fix it. Thanks in advance.
Hope you have the "stdafx.h" file that you are trying to include in the right path. The code works fine without the file being included.

c++- variables to system()

I am working on a console program for Windows, and one of my settings is an option to change the console and text color.
I am using c++, so I can do something like system("color 07");, which will make the background black and the text white.
What I want to do is to present all 16 color options, and then let the user take his pick.
(Below is a portion of my code):
int a;
int b;
cout << "Please enter your background color." << endl;
cin >> a; //the user inputs 0
cout << "Please enter your text color." << endl;
cin >> b; //the user inputs 7
How to I pass the two variables to the system() call? I googled around, but all I could find were string to system(), which I do not want.
Also, I am very well aware of how evil system() is, so if anyone has other options other than system() that will do the same thing, that would be fine. But please do not tell me how evil system() is.
Thanks in advance!!
The system command takes a single const char* parameter. Therefore you just need to build a string for the command you wish to execute.
#include <iostream>
#include <sstream>
#include <string>
int main()
{
int backgroundColor;
std::cout << "Enter background color\n";
std::cin >> backgroundColor;
int foregroundColor;
std::cout << "Enter foreground color\n";
std::cin >> foregroundColor;
std::stringstream stream;
stream << "color " << backgroundColor << foregroundColor;
std::cout << "Command to execute: '" << stream.str() << "'\n";
::system(stream.str().c_str());
return 0;
}
This could be a simpler solution using C++ constructs.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
string bgClr,fgClr;
cin >> bgClr >> fgClr;
::system((bgClr+fgClr).c_str());
return EXIT_SUCCESS;
}
char command[500] = "";
sprintf(command, "color(%d, %d)", a, b);
int result = system(command);

Cout of a string is giving an error and a hard time some insight help pls?

I cant find the error in this piece of code could anyone please insight me? I ran the debugging but the errors are un-understandable..
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
string name;
cout << "Input your name please?" << endl;
cin >> name;
if
{
(name == "Bart Simpson")
cout << "You have been very naughty" << endl;
}
return 0;
}
Problems:
You have some missing #includes, which probably caused your initial compiler errors.
You have a simple syntax error with your if statement.
Using the stream extraction operator will never yield a string with whitespace inside of it.
The following should work as you expect:
#include "stdafx.h"
#include <iostream>
#include <ostream>
#include <string>
using namespace std;
int main()
{
cout << "Input your name please?" << endl;
string name;
getline(cin, name);
if (name == "Bart Simpson")
{
cout << "You have been very naughty" << endl;
}
return 0;
}
(You need to include string for std::string and std::getline, and ostream for std::endl.)
I assume the bracket in the wrong place is just a problem when pasting the code
if(name == "Bart Simpson")
name will never equal "Bart Simpson", since extracting a string stops when it encounters whitespace; so it would only be "Bart". Perhaps you want to use getline() instead?
Should be
if (name == "Bart Simpson")
{
cout << "You have been very naughty" << endl;
}
And you need to include <string>