Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

4
Beginner c++ help, math equations in string objects?
Post Flair (click to view more posts with a particular flair)
Post Body

Hello all, So I recently started learning c for school, and I've written this program to try and average three months of rainfall. My problem is stemming from the fact that with this variation of the code i get this error: "binary '/': 'std::basic_string<char,std::char_traits<char>,std::allocator <char>>' does not define this operator or a conversion to a type acceptable to the predefined operator" which makes me think that maybe i can't do math with the string operator? Not sure how to fix this. When i try it without the "/" and just the addition of the rainfall numbers, it doesnt add them and just stack them together so instead of 1 2 3 =6 it goes 1 2 3 = 123. Any help would be much appreciated.

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

int main()
{
    string month1;
    string month2;
    string month3;
    string total;
    string rainfall1;
    string rainfall2;
    string rainfall3;

    cout << "This program calculates average rainfall in inches from three different months. \n";
    cout << "First month: "; 
    cin.sync();
    getline(cin, month1);
    cout << "Second month:";
    cin.sync();
    getline(cin, month2);
    cout << "Third month:";
    cin.sync();
    getline(cin, month3);
    cout << "Rainfall for first month: ";
    cin.sync();
    getline(cin, rainfall1);
    cout << "Rainfall for second month:";
    cin.sync();
    getline(cin, rainfall2);
    cout << "Rainfall for the third month: ";
    cin.sync();
    getline(cin, rainfall3);

    total = (rainfall1   rainfall2   rainfall3) / 3;

    cout << "Average monthly rainfall for " << month1 << ", " << month2 << ", and " << month3;
    cout << " is " << total << " inches\n";

    return 0;
}

Author
Account Strength
100%
Account Age
11 years
Verified Email
Yes
Verified Flair
No
Total Karma
20,860
Link Karma
4,909
Comment Karma
12,086
Profile updated: 1 week ago
Posts updated: 1 month ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
8 years ago