My first program

So, I recently found myself growing a passion for learning the c++ language after reading how hard it is (I like a challenge) and found myself giving it ago in Visual Studio. Anyway; this is my first (and second) console program. This programme allows users to find the circumference, radius and area of a circle.

The second console application (though based off of the same code type) converts celsius into fahrenheit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  #include <iostream> 
#include <cmath>
#include <math.h>



const double PI = 4.0*atan(1.0);

int main() {
	//Enable user to input input data..
	double radius, circumference, area;
	std::cout << "Work out the radius, circumference and area of a circle!" << std::endl;

	/*Let user use multiple times
	 *if user inputs a value that is
	 *negative close program*/
	while (true){
		std::cout << "Enter the radius" << std::endl;
		std::cin >> radius;
		circumference = 2 * PI* radius;
		area = PI * radius * radius;
		std::cout << "The circumference is: " << circumference << "cm" << std::endl;
		std::cout << "The radius is: " << radius << "cm" << std::endl;
		std::cout << "The area is: " << area << "cm" << std::endl;
		//reset user input
		std::cin.ignore();
		std::cin.get();
		//close if value is negative
		if (radius == -1){
			break;
		}
	}
}



//This is the second application:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// TemperatureConverter.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


int main(){

	double celsius, fahrenheit;
	double userInput;
	while (true){
		std::cout << "Please enter a value in celsius: " << std::endl;
		std::cin >> celsius;
		fahrenheit = (celsius * 1.8) + 32;
		std::cout << "Your temperature " << celsius << " degrees celsius has been converted. " << std::endl;
		std::cout  << celsius << " degrees celsius is equivalent to: " << fahrenheit << " degrees fahrenheit." << std::endl;

		std::cout << "Press Enter to convert another value or press \"E\" to exit" << std::endl;
		std::cin >> userInput;

		if (userInput == 'e' || 'E') {
			break;
		}

		return 0;

	}

}



With the second program: how can I make it continue to loop after user has inputed "e" || "E"? When I try to run it it will allow me to press enter but when I enter a value then press enter it still closes?

But other than that; how have I done on my first ever piece of code in c++?
Remove the return 0; on line 26 of second listing
Also, the condition (userInput == 'e' || 'E') will always be true, no matter what the value of userInput.
Use char for character input char userInput;

I replaced std::cin >> userInput; with
1
2
std::cin.ignore();
std::cin.get(userInput);
because the >> operator gets what you've entered before the enter. Since by pressing enter no character is before it, cin 'waits' until you enter a character. The get function gets whatever the inputed character is, so it does he's job here.
std::cin.ignore(); deletes the '\n' which remains in the buffer after reading celsius value.

Finally, I used do-while instead of while because userInput has no value at the beginng. You could initialize userInput with something that is not 'e' or 'E' and use the normal while loop instead.

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// TemperatureConverter.cpp : Defines the entry point for the console application.
//

#include <iostream>


int main(){

	double celsius, fahrenheit;
	char userInput;

	do{
		std::cout << "Please enter a value in celsius: " << std::endl;
		std::cin >> celsius;
		fahrenheit = (celsius * 1.8) + 32;
		std::cout << "Your temperature " << celsius << " degrees celsius has been converted. " << std::endl;
		std::cout  << celsius << " degrees celsius is equivalent to: " << fahrenheit << " degrees fahrenheit." << std::endl;
		std::cout << "Press Enter to convert another value or press \"E\" to exit" << std::endl;
		
		std::cin.ignore();
		std::cin.get(userInput);
		
	}
	while (userInput!='e' && userInput!= 'E');

		return 0;

}
Thanks for your input guys! Are there any good books you could recommend for me? I don't know where I can go from here =/
closed account (91vUpfjN)
Couldn't he have just used continue instead of break?
The books listed under 'Beginner Introductory' here:
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
are all good.

A decent online tutorial: http://cppannotations.sourceforge.net/annotations/html/
closed account (iAk3T05o)
Always initialize your variables, always.
1
2
3
4
double fahrenheit = 0,
celsius = 0;
char userInput = ' ';
//do same to others 

you may think it adds more code but you don't want an error and warning message of uninitialized variable to appear.
"Nathan2222" Is there any specific reason as to why the variables need to be initialised to '0'? Or can it be any number? Apart from the ones in which a definitive value should be added such as var x = 10; (obviously) (Absolute "newb" question I'm sure, just checking though :))

Will do from now on; lesson learned.

"JLBoreges" thanks bud! Will have a look at some of them, see which one I get on with better.

On the StackOverflow link: do you know if the c++ primer 5th edition is the latest one? I'm thinking of buying that, just want to make sure that it's the newest version, thanks everyone!
Last edited on
Is there any specific reason as to why the variables need to be initialised to '0'? Or can it be any number?

It doesn't have to be zero - it can be whatever you think is a sensible, helpful initial value for the variable to have.

The point is to have your program in a known, well-defined state whevever possible.
> Is there any specific reason as to why the variables need to be initialised to '0'? Or can it be any number?

That is the wrong question.

To the right question: "Is there any specific reason as to why we must try to postpone the definition of a local variable till we know how to initialize it?"

The answer would have been: Improved program clarity and correctness, and in non-trivial cases efficiency. See Item 26 here: http://books.google.co.in/books?id=7x2MxvbjGsYC&pg=PT141&lpg=PT141

With this guideline in operation, the first program might have been written like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>

const double PI = 4.0*atan(1.0);

int main() {

    std::cout << "Work out the radius, circumference and area of a circle!" << std::endl;

    while(true) {

        std::cout << "Enter the radius (enter a negative value to quit): " ;
        double radius ;
        std::cin >> radius;
        if( radius < 0 ) break ;

        const double circumference = 2 * PI* radius ;
        const double area = PI * radius * radius;

        std::cout << "The circumference is: " << circumference << "cm" << std::endl;
        std::cout << "The radius is: " << radius << "cm" << std::endl;
        std::cout << "The area is: " << area << "cm" << std::endl;
    }
}
Topic archived. No new replies allowed.