简体   繁体   中英

How to make a variable input be any number I put in (C++)

I'm new to coding and am having trouble with one of my first projects.

So I have to basically type a number and then have that number spit out some answers involving things like addition and multiplication. (ex. if I put in 5, one of the answers is to simply multiply that 5 by 30. Or another adds it).

The issue is that the only way I can get that number to be different and give the corresponding answers is if I go into the code and change the integer assigned to the variable, which is pretty counterintuitive.

I've tried deleting the variable, setting = 0, moving it around; nothing is working.

stocks = 0;
investment = stocks * 30;
charges = investment * .015;
total = charges + investment;
cout << "Cindy, how much are you investing?";
cin >> stocks;
cout << endl;

some of the code there. Line in question is the "stocks = 0;" one. After that, it's the other variables that would be output with their associated variables. Any help would be appreciated, I hope this makes sense for what I'm asking! Thanks in advance

You will have to cin the number.

#include <iostream>

int main() {

    int stocks;
    std::cout << "Cindy, how much are you investing?";
    std::cin >> stocks;
    investment = stocks * 30;
    charges = investment * .015;
    total = charges + investment;
    std::cout << "The total charges would be " << total << "." << std::endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM