How Can I Use Setw() with Input Stream [Closed]
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 #Include...
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: