Variablesand Constant

Hello guys! I am very new to C++ coding and I have a huge question. I believe you fellows can assist me. Well im currently developing a program and lets say this. This is an example

//declare a variable called apples which can hold small decimal numbers
float apples;
//assign apples the value
apple = 1.00
//promt the user to enter "How many apples would you like to purchase?"
cout << "How many apples would you like to purchase?"


haha, what I'm asking is this the corrcet way? or can I not have the constant and the variable spelt the same way. I want it to work like this.

I want apples to hold the value of one dollar, but for the user to specify how many they'd like, any feed back is welcomed. If anything if someone could perhaps give me an example of how it should be properly coded too that would be great. Thanks for considering my question


Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main()
{
    // use double as the default floating point type
    // if the value is known, and is immutable (does not change),
    // make it const and initialise it (ie. do not try to assign to it)
    const double unit_price_of_apple = 1.00 ;

    int number_of_apples ; // not a const (value not known till later)
    std::cout << "How many apples would you like to purchase? " ;
    std::cin >> number_of_apples ;

    // now that we have both pieces of information
    // a. verify that number_of_apples is positive
    // b. compute total cost of purchase
    // etc.
}
Topic archived. No new replies allowed.