Fred Suddell
Fred Suddell

Reputation: 11

Is my understanding of how a variable works, wrong?

enter image description here

Hello. I am very new to C++ and programming in general. I am trying to build my confidence by playing around with what small components I already understand until I am as familiar as I can be before learning more. However I am stumped and getting frustrated at myself because I cant seem to make this simple piece of code work. I hope that whoever reads this understands what I was trying attempt and can tell me where I have gone wrong. please and thank you.

#include <iostream>

int main(int argc, const char * argv[]) {


    char computer[100];
    char something[100];



    double price;
    double price2;
    double total = (price + price2);
    double budget;
    if (budget < total){ double need = total - budget;}
    if (budget > total ){ double surplus = budget - total;}
    double perday = total / 365;
    double remain = budget - perday;
    int A;

    std::cout << "Please tell me, the 'name' of the new computer that you would like to buy.\n";
    std::cin >> computer;
    std::cout << " How much does this " << computer << " cost?\n";
    std::cin >> price;
    std::cout << "Cool! Now tell me something else you would like to buy.\n";
    std::cin >> something;
    std::cout << "How much does " << something << " cost?\n";
    std::cin >> price2;
    std::cout << "I need to know your technology budget:\n £";
    std::cin >> budget;


    if (budget >= total)
    {std::cout << "Wow you have enough cash to get both " << computer << " and " << something << "\n with a surplus budget of £" << surplus;}

    else (budget < total) {
    std::cout << "I am sorry you lack the necessary funds right now. \n";
    std::cout << "Would you like to hear a payment plan?\n 1 for yes / 2 for no \n";
    std::cin >> A;
    }

    if (A = 1) {
            std::cout << "If you wanted to buy both " << computer << " and " << something << " \n by this time next year, you could pay £" << perday << "each day from now\n ";
            std::cout << "If I take today's payment now, you will have £" << remain << "left of your budget";}
        else return 0;}
}

Upvotes: 0

Views: 87

Answers (1)

anthino12
anthino12

Reputation: 968

first of all declaring a variable within a scope is a bad habit except you plan to use that variable only in that scope.

if (budget < total){ double need = total - budget;}
if (budget > total ){ double surplus = budget - total;}

In this case, need and surplus live only between these { } and can't be used outside of them. If you want to use them later in your program, make sure you declare them out of the scope.

The other thing I noticed is that assigning operator = in programming language is used for adding values to a variable. In your case, if (A = 1) won't work because cpp won't check whether A is 1 but it'll assign A to be 1. What should you do is check if (A == 1) with double ==. Check here for operators

I suggest reading a good book for cpp and getting better on that way.

If you're wondering, here's a working version of your program

Upvotes: 2

Related Questions