By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,793 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,793 IT Pros & Developers. It's quick & easy.

Please help.. cin is not giving me anything

P: 3
This is what I'm trying to do:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int membername, oweight, lostw, percentlost;
double totalwt;

cout << "What is the member's name? ";
cin >> membername;
cout << "How much did you weight?\n ";
cin >> oweight;
cout <<"How many pounds have you lost?\n";
cin >> lostw;
percentlost = (lostw / oweight) * 100;
totalwt = oweight - lostw;
cout << "The member " << membername << " has lost " << percentlost<< "% of weight and now weight " << totalwt << "pounds";

return 0;
}

I'm able to enter the member's name, but that is it.
Please help.
Mar 3 '19 #1

✓ answered by dev7060

You declared "membername" as int type but I am pretty sure you must be providing a string type value. Declare "memebername" as a string array and it'll be fine.

Share this Question
Share on Google+
4 Replies


dev7060
100+
P: 115
You declared "membername" as int type but I am pretty sure you must be providing a string type value. Declare "memebername" as a string array and it'll be fine.
Mar 3 '19 #2

P: 3
Thank you! it worked, but now I'm stuck in the calculations:

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main()
{
string membername;
string oweight;
string lostw;

double totalwt;
double percentlost;

cout << "What is the member's name? ";
getline(cin, membername);
cout << "How much did you weight? ";
getline(cin, oweight);
cout << "How many pounds have you lost? ";
getline(cin, lostw);

percentlost = (lostw / oweight) * 100;
totalwt = oweight - lostw;

cout << "The member " << membername << " has lost " << percentlost << "% of weight and now weight " << totalwt << "pounds";

return 0;
}

It shows that "/" and :-"
error C2676: binary '/': 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator :/
Mar 3 '19 #3

dev7060
100+
P: 115
You need to look at data types and their correct usage.

Here, "membername" needs to be a string, so we declared it as string data type. But both "oweight" and "lostw" will contain numeric values, so they need to be declared as int/float/double according to the need.

Since you declared "oweight" and "lostw" as strings, you are treating them as strings and hence, using getline() function to store string type values in them.
Later on you are using operators that are not meant to be used with string type values. The error you got is all about this.

Here's what you have to change:
- Declare oweight and lostw as int/float type.
- Replace the getline() function with the simple console input keyword (cin) for them.
It should be working now.
Mar 3 '19 #4

P: 3
It works dev7060! Thank you, I really appreciate your help :)
Mar 4 '19 #5

Post your reply

Sign in to post your reply or Sign up for a free account.