I took an input as string with scanf() but output with cout.why did my program terminate? - string

I took an input as string with scanf() but output with cout.My program terminated.But if I use printf() instead of cout it works.Can anybody tell me about this ?
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
scanf("%s",s);
cout <<s<<endl;
}

scanf uses c-string (char arrays).
Use getline instead.

Related

I used cin to get a string input as shown below in the code and it works fine. Is it alright?

#include<iostream>
#include<stdio>
using namespace std;
int main()
{ int n;
char s[15];
cin>>n;
cin>>s;
cout<<n*2<<"\n";
cout<<s;
return 0;
}
I tried with gets and fgets function but they don't work just after cin..
I'm kind of confused on what you are asking here, but I have noticed something here that can be fixed.
Yes the code you have compiles and it works. However, it could be improved.
When prompted to input something to your char array, you'll notice that it will not accept whitespaces. So if I input, Jon Smith, the output will only be Jon and the rest of the string input is cut off. To fix this, you will need to make a call the the getline() function.
The documentation of getline() states:
Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n'..)
This will allow you to get whitespaces from the input and put the entire input back into a string.
If you add this function call to your code where the second input prompt lies and you were to run the code, you would notice that you will only get prompted once and then the program would finish running before the second prompt appears to be executed. This is because getline() does not ignore leading whitespace characters and it stops reading any further because the cin>> before it is seen as a newline character.
To make getline() work with cin>>, you must use cin.ignore() before the call to getline(). Below is some code that I wrote to make this adjustment:
// Example program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
string s; //using string allows us to use getline()
cout<<"Enter a number: "; //Let user know they are being prompt for number
cin>>n;
cin.ignore(); //ignore the leading newline
cout<<"Enter a string: "; //let user know being prompt for string
getline (cin,s);
cout<<n*2<<"\n";
cout<<s;
return 0;
}
Again, the code you have works and compiles. I'm not sure if my solution is the answer you are hoping to get but I hope that you are able to find this useful! Cheers!

Unable to use Iterator for a MAP to PRINT STRING. (able to print int)

I tried printing a string in the MAP using iterator but I am getting an error.
Program -
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<map>
#include<string.h>
#include<unordered_map>
using namespace std;
void main()
{
std::map<int,std::string>mymap;
std::map<int,std::string>::iterator it;
mymap.insert(make_pair(10, "sid"));
mymap.insert(make_pair( 20, "sam"));
for (it = mymap.begin(); it != mymap.end(); it++)
{
//printf("%s \n", it->second);
std::cout <<*it->second << std::endl;
}
system("pause");
_getch;
}
Error list
1.Error C2679 binary '<<': no operator found which takes a right-hand
the operand of type 'std:: string' (or there is no acceptable conversion)
2.Error (active) no operator "<<" matches these operands
I was able to print the int properly. I am unable to print the STRING. Please suggest a solution.
That's not how you access the object pointed to by the iterator.
Just use cout<< it->second<<endl;.
All the errors are then automatically resolved.
P.S. Refer to my comment on your question.

How to round a number to the nearest hundreath and make that display on a string in c++

I am working on a money program. Currently I am trying to make a part in the program that displays a list that shows every transaction that was made in the session. My problem is that when I convert the money amount to a string instead of displaying something like 100.35 in when converted to a string it instead displays something like 100.35000000. I was wondering if there was any way I could make the program drop the additional zeros? Here is a sample of how I convert the numbers to a string
int main(){
double samplemoney=100.35;
string sample="Today we made $";
string comsample;
comsample=sample+std::tostring(money)+".";
cout<<comsample<<endl;
return 0;
}
In my main program this part is handled with a class but as I said earlier it seems like no matter what the money value I put in is it will display a series of zero and I want my program to drop the unnecessary zeros.
Let's say you have this number:
double number = 190.0391000;
If the problem is displaying the value you may use
std::cout << std::setprecision(5) << number
where f is the number you want to show.
If your problem is having a string with a finite precision you can use sprintf() in the following way:
char arr[128];
sprintf(arr,"%3.2f", number);
std::string formattedstring(arr);
or in a more C++ oriented way something like this
std::stringstream strn;
strn.precision(2);
strn << std::fixed << number;
and then you get the string in the following way:
std::string formattedstring = strn.str();
I attach the full text of program here... tested on my machine:
#include <sstream>
#include <iostream>
int main() {
float number=100.24324232;
std::stringstream strn;
strn.precision(2);
strn << std::fixed << number;
std::cout << strn.str() << "\n";
return( 0 );
}

how to convert mpf_class to String

Hello and sorry for my basic English. I'm trying to convert from mpf_class to a String. I know there is a function (get_str()) but it show me only digits and its exponent separated. I want to get the whole expression in a string. I tried using ostreamstring and it work but I want to know if there is another way to do that. Let me know if I made myself clear.
Basically what I did was:
std::ostringstream show;
mpf_class result, Afact,Bfact,Cfact;
result=Afact*Bfact/Cfact;
show << result;
ui->lineEdit_5->setText(QString::fromStdString(show.str()));
As you can see, I'm working in a QT project and I need to show the result in a QLineEdit and with ostreamstring it works. I just was wondering if there is a gmp function to do that. thanks
Not sure whether this can help you, but you can actually print an mpf_class object and use I/O manipulators on it as a typical float object.
Here is my code
#include <gmpxx.h>
#include <iostream>
#include <iomanip>
int main(void) {
mpf_class a;
a = 3141592653589793.2;
std::cout << a << std::endl;
// Outputs 3.14159e+15
std::cout << std::uppercase << std::showpos << std::setprecision(3) << a << std::endl;
// Outputs +3.14E+15
}
Then you can use an std::ostringstream object instead of std::cout.
Reference: https://gmplib.org/manual/C_002b_002b-Formatted-Output.html

How to handle strings in VC++?

HI,
Once we accept an input from a keyboard, How can we add that character into a string in VC++?
Can anyone help me with this?
You can use std::string from STL, and the + or += operator.
To do this, #include <string> and use the class std::string.
After that, there are various ways to store the input from the user.
First, you may store the character input directly into the string:
std::string myStr;
std::cin >> myStr;
Second, you can append the input to an existing string:
std::string myStr;
myOtherStr += myStr;
#include <iostream>
#include <string>
int main(int argc, char**argv)
{
std::string s;
std::cin >> s;
s += " ok";
std::cout << s;
return 0;
}
Try the following:
std::string inputStr;
std::cin >> inputStr;
This code will accept a string typed on the keyboard and store it into inputStr.
My guess is that you're in the process of learning C++. If so, my suggestion is to continue reading your C++ book. Keyboard input will surely be addressed in some upcoming chapter or the other.
There are several ways to go about, depending of what you exactly need. Check the C++ I/O tutorial at this site: www.cplusplus.com

Resources