This question already has answers here:
How does std::flush work?
(4 answers)
Optimizing away a "while(1);" in C++0x
(9 answers)
Closed 3 years ago.
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout << "What is your name? ";
getline (std::cin, name);
std::cout << "Hello, " << name << "!\n";
while (true);
std::cout<<"over";
}
Program outputs “What is your name” and I input my name, say “Dom”, but the program then waits there. Why doesn’t it show “Hello Dom!” before it gets stuck at the while loop?
Related
This question already has answers here:
How do you reverse a string in place in C or C++?
(21 answers)
Closed 7 years ago.
Hey guys I'm new here and a programming noob so bear with me here.
This is for my C++ class which sadly my teacher is terrible at teaching so many things confuse me so I need some help here.
We have a lab that is called 'Reverse Sentence' and this is what it wants In this lab.
Write the function "ReverseSentence" that takes a string parameter and changes it, by reversing it.
For example:
INPUT: the first test
OUTPUT: tset tsrif eht
The function must not use an extra string, but must reverse the elements of the input string.
#include <iostream>
#include <string>
using namespace std;
void ReverseSentence( string& inputSentence){
/* Body of Function Here */
}
int main(){
string inputSentence;
cout << "Input your sentence: ";
getline(cin, inputSentence);
cout << endl;
ReverseSentence(inputSentence);
cout << "Reversed Sentence:" << endl;
cout << inputSentence << endl;
return 0;
}
Can someone please help me what function is because I'm having trouble with it.
Just use std::reverse:
void ReverseSentence( string& inputSentence){
std::reverse(inputSentence.begin(), inputSentence.end());
}
Half of the cycle and swap.
#include<algorithm>
#include<string>
void ReverseSentence(std::string &s){
for (int i = 0; i < s.size()/2; ++i)
std::swap(s[i], s[s.size() - i - 1]);
}
This question already has answers here:
Wait until user presses enter in C++?
(3 answers)
Closed 7 years ago.
I am a total newbie to C++ and I need to have 3 line of text and after every line I have to ask the user to press enter to continue. How can I do it?
Here is the code that I have so far:
#include <iostream>
using namespace std;
int main()
{
std::cout << "Es esmu dators.";
std::cout << "Es zinu C++.";
std::cout << "C++ ir programmesanas valoda";
}
You can use getchar() after each line. To use getchar() you must include cstdio.
Example code:
#include <iostream>
#include<cstdio>
using namespace std;
int main()
{
std::cout << "Es esmu dators.\n";
getchar();
std::cout << "Es zinu C++.\n";
getchar();
std::cout << "C++ ir programmesanas valoda\n";
}
From this answer:
Several ways to do so, here are some possible one-line approaches:
Use getch() (need #include ).
Use getchar() (expected for Enter, need #include ).
Use cin.get() (expected for Enter, need #include ).
Use system("pause") (need #include ).
PS: This method will also print Press any key to continue . . . on the screen. (seems perfect choice for you :))
You should also perform a quick search on the site to see if your question has been asked before, going forward.
This question already has answers here:
execute C++ from String variable
(6 answers)
Closed 7 years ago.
Hi I have been trying to make a program that will ask for the users input and whatever the user types it will execute it as a command. Sort of like CMD
#include <iostream>
#include <string>
using namespace std;
string A;
int main(){
for (int i = 0; i > -1; ++i){
cout << "Command: ";
cin >> A;
// Here Would Be The Code
cout << "Command Executed!";
}
}
Here is what i imagine a possible output (if it worked)
Command: cout << "Test";
Test
Command Executed!
You can use the system function.
http://www.cplusplus.com/reference/cstdlib/system/
system("command")
This question already has answers here:
std::cin input with spaces?
(8 answers)
Closed 8 years ago.
The code is not giving desired output
when I type in a string example "Ben Parker", the output is "Goodmorning, Ben" and not the entire name("Ben Parker") what seems to be the problem?
#include <iostream>
#include <stdlib.h>
#include <cstring>
int main() {
char your_name[20];
std::cout << "Enter your name: ";
std::cin >> your_name;
std::cout << "Goodmorning, ";
std::cout.write (your_name, strlen(your_name)) << std::endl;
return 0;
}
SOLUTION
This was a very old question when I just began programming.
The entire character array can be read and printed with a for loop, or better a string type variable can be used, since it is C++.
using string your_name; seems to fix the problem, which can be then printed with a simple std::cout << your_name << endl;
You probably put a space in between "Ben" and "Parker" in input. This would cause the cin logic to believe it had an answer after seeing the space following "Ben". You will probably want to read an entire line at a time to get past that problem. See this page for an example.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Alternative to itoa() for converting integer to string C++?
I want to convert an int variable to string to put it in the textbox.
I have tried this code:
int x = 123;
std::ostringstream osstream;
osstream << x;
std::string string_x = osstream.str();
But it doesn't work.
Try using stringstream instead
std::stringstream osstream;
osstream << x;
std::string string_x = osstream.str();
std::cout << string_x << std::endl;
Works on my computer.
I had another issue once where I had to append a whitespace to that, so try this too:
std::stringstream osstream;
osstream << x << " ";