Please help.. cin is not giving me anything

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newbi
    New Member
    • Mar 2019
    • 3

    Please help.. cin is not giving me anything

    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.
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

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

    Comment

    • newbi
      New Member
      • Mar 2019
      • 3

      #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 :/

      Comment

      • dev7060
        Recognized Expert Contributor
        • Mar 2017
        • 656

        #4
        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.

        Comment

        • newbi
          New Member
          • Mar 2019
          • 3

          #5
          It works dev7060! Thank you, I really appreciate your help :)

          Comment

          Working...