How can I use setw() with input stream [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I know some people will say this question is repeated but I really couldn't find a useful answer.
Let's say I have the following program:
#include <iostream>
#include <string>
using std::cout;
int main(){
std::string something;
cout<<"Type something";
std::cin>>something;
}
How can I use setw() so the output will look like this?
Type something "then after some whitespaces for example 10 the user will start typing"
I tried to use setw() in output:
#include <iostream>
#include <string>
#include <iomanip>
using std::cout;
int main(){
std::string something;
cout<<std::left<<std::setw(24)<<"Type something";
std::cin>>something;
}
The expected output was supposed to be:
The actual output is:

I can't reproduce what you say, the code you have showed works fine for me.
#include <iostream>
#include <string>
#include <iomanip>
int main(){
std::string something;
std::cout << std::left << std::setw(24) << "Type something"; // prints "Type something "
std::cin >> something;
return 0;
}
That said, you could simply output a string with the desired number of spaces:
#include <iostream>
#include <string>
int main(){
std::string something;
std::cout << "Type something ";
// alternatively:
// std::cout << "Type something" << std::string(10, ' ');
std::cin >> something;
}

One possible solution might be using any special character after setw
Sample Code:
int main()
{
std::string something;
cout<< "Type something" << setw(24) << ":";
std::cin>>something;
}
Output:
Type something :inputString
References:
iomanip setw() function in C++ with Examples
Setw C++: An Ultimate Guide to Setw Function

Related

can someone explain to me why my string is not not showing in output it happens to me a lot and this is a simple example, [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
can someone tell me why s is not showing up.
#include <string>
#include <iostream>
using namespace std;
int main()
{
string a="1 23";
string s="";
if(a[1]==' '){s[0]=a[1];
cout<<s; }
return 0;
}
You are not allocating any character memory for s to refer to, so s.size() is 0 and thus [0] is out of bounds, and writing anything to it is undefined behavior 1.
1: in C++11 and later, you can safely write '\0' to s[s.size()], but you are not doing that here.
Try this instead:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string a = "1 23";
string s = "";
if (a[1] == ' ') {
s += a[1];
// alternatively:
s = a[1];
// alternatively:
s.resize(1);
s[0] = a[1];
cout << s;
}
return 0;
}
Just note that a[1] is the whitespace character ' ', so while you can assign that character to s, and it will print to the console, you just won't (easily) see it visually in your console. Any of the other characters from a would be more visible.
This assignment
if(a[1]==' '){s[0]=a[1];
invokes undefined behavior because the string s is empty and you may not use the subscript operator for an empty string to assign a value.
Instead you could write for example
if(a[1]==' '){s += a[1];
Pay attention to that in this case the string s will contain the space character. To make it visible you could write for example
#include <iostream>
#include <iomanip>
#include <string>
//...
std::cout<< std::quoted( s ) << '\n';
or
std::cout << '\'' << s << "\'\n";
My guess is that this is a naive attempt to parse the string. Is this what you are looking for?
#include <string>
#include <iostream>
using namespace std;
int main()
{
string a = "1 23";
string s;
if (a[1] == ' ') {
s = a.substr(2);
cout << s;
}
return 0;
}
Thats because "s" is empty string with size 0.
It means there is no element at index [0] in it.
When you ask "s" for [0] element it leads to UB.
If you want to add a[1] to "s", you can use:
s.push_back(a[1]);
Or:
s += a[1];
Or:
s.insert(s.begin(), a[1]); // That's bad way, dont use it.

How to display user input string when questions ask for input in reverse order in C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I recently started learning C++. I must create a program that asks the user for their name and a file name after; then displays the file name with type (eg .cpp) 1st and the name after. Must be able to accept negative values.
I'm using repl.it to write the code 1st then pasting in to a .cpp file to compile. Then will compile it with the makefile using the command make hello and check that the generated program compiles and runs correctly.
Sample run:
What's your name? John
What's the name of the output file? gen
gen.cpp:
#include <iostream>
int main() {
std::cout << "Hello John!\n";
}
Attempt:
#include <iostream>
using namespace std;
int main ()
{
string a;
cout << "What's your name?\n";
string b;
cout<< "Whats the name of this file?";
getline (cin,b);
cout<<"//"<<b;
getline (cin, a);
cout << "Hello " << a;
return 0;
}
Except it displays in the console:
What's your name?
Whats the name of this file? //e.g.c
//c
The output is wrong, so what mistake did I make? What's the correct method to get the expected values?
Edit: This is what the assignment says:
As mentioned in comments, your calls to getline() are in the wrong places. Try this instead:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string username;
cout << "What's your name?\n";
getline (cin, username);
string filename;
cout << "Whats the name of this file?";
getline (cin, filename);
cout << "//" << filename << ".cpp" << endl;
cout << "Hello " << username << endl;
return 0;
}

Reading from a .txt file to a vector of doubles in C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to read values from a .txt file to a vector (which is a member of a class) in C++, but despite the .txt having around 1000 lines, the vector is of size 0. I inserted a 'cout', and I know the file is opened and closed. I'm not sure what I could be doing wrong for the code not to read the contents of the .txt.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include "option_class.h"
using namespace std;
int main(){
double cprice = 0.0;
int i = 0;
string line;
ifstream is;
is.open("/Users/<USER>/Desktop/SPY.txt");
if (!is){
cout << "Unable to open file" << endl;
return(0);
}
while(!getline(is, line).eof()){
is >> cprice;
option1.price.push_back(cprice);
}
is.close();
cout << "Closing file" << endl;
}
Have you tried something simpler:
while (is >> cprice)
{
option1.price.push_back(cprice);
}
The operator>> will skip whitespace, which includes newlines. There is no need to read a line at a time.

Enter a string and divide it in words [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I need to code a program based which can compare words of a sentence or paragraph to a database, as if it were a text corrector. My problem here is that I have to enter on the console the text I want to correct as a string and then divide it in words stored in a vector of strings in C++. I tried a thousand ways but I cannot get it done.
Here is the code I last tried:
std::cout << "Enter the text: ";
std::string sentence;
std::vector<std::string> vText;
while (getline(std::cin, sentence)){
std::stringstream w(sentence);
std::string word;
while(w >> word)
vText.push_back(word);
}
When I execute this code, I got nothing, as if the program did nothing. Can you help me please?
This is the final thing (a piece):
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
int main(){
std::cout << "Introduzca una frase: ";
std::string frase;
std::vector<std::string> vTextoAnalizar;
while (getline(std::cin, frase)){
std::stringstream w(frase);
std::string palabra;
while(w >> palabra)
vTextoAnalizar.push_back(palabra);
}
for (int i=0;i<vTextoAnalizar.size();i++){
std::cout << vTextoAnalizar[i] << std::endl;
}
return 0;
}
Firstly, welcome to stack exchange. Your question has been down voted as you have not made a reasonable effort to ask a good question.
Please take particular note of How to create a Minimal, Complete, and Verifiable example.
I think what you are trying to do is something like this:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
std::vector<std::string> read_text() {
std::cout << "Enter the text: ";
std::string sentence;
std::string word;
std::vector<std::string> vText;
while(getline(std::cin, sentence)){
std::stringstream ss(sentence);
while ( getline( ss, word, ' ' ) ) {
if (word.compare("quit") == 0)
return vText;
vText.push_back(word);
}
}
}
int main() {
std::vector<std::string> test_vector = read_text();
std::cout << "Vector : " << std::endl;
for (int i=0;i<test_vector.size();i++){
std::cout << test_vector[i] << std::endl;
}
}
This will split on spaces, and add your words to the vector at the end of each sentence. I imagine that there are more intelligent ways of parsing, but this should get your test code working.
Hope that helps.

Understanding stringstream [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have never used stringstream before and was given a sample code but with no explanation of what was happening in the code. If someone could explain each line's purpose that would be great. I have looked in multiple places but cant seem to pin down the second line.
#include <sstream> // i know this line includes the file
stringstream ss(aStringVariable);// this line in particular
ss >> aVariable;
getline(ss, stringVariable2HoldValue, ‘|’);
There's a constructor for std::stringstream that takes a std::string as a parameter and initializes the stream with that value.
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::stringstream ss("foo bar");
std::string str1, str2;
ss >> str1 >> str2;
std::cout << "str1: " << str1 << std::endl;
std::cout << "str2: " << str2 << std::endl;
}
This code initializes a stringstream, ss, with the value "foo bar" and then reads it into two strings, str1 and str2, in the same way in which you would read from a file or std::cin.