My code gives me a long small number.

My code gives me a long small number in "Program 5"
Then "Program 6" messes up and does not even run properly.

This is just to practice but I cant get Program 5 and 6 to work correctly.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Introduction to C++ Getting Started 

Programs:
1.	Create a program that announces “Good Morning, Planet Earth!” to the user.
2.	Of course, in the event that it’s late at night, create another program that announces, “Good Evening, Planet Earth!”
3.	Create a program that asks the user for their height in feet, and stores that into an int variable. 
Print out their height in inches.
4.	Create a program that asks the user for their weight in pounds. In the United Kingdom, 
it is common to express a person’s weight in “stone”, which is approximately 0.0714 stone to the pound. Print out their weight in stone.
5.	Find out today’s spot price for gold per troy ounce (or just use US$1000). 
One pound of weight is equal to approximately 14.583 troy ounces. 
     Create a program that asks the user for their weight in pounds, then prints out:
You are worth US$worth in gold!
Calculate “worth” by multiplying the weight by 14.583 and the price of gold.
6.	Create a program that first asks the user for their first name, second asks for their middle initial, and then finally asks for their last name. 
   Print out their name as:

Your name is: last name, first name middle initial

where, of course, the actual last name, first name and middle initial are used. 
Remember that people’s names very often more than one word, so you’ll need to read an entire line at a time.
*/

#include <iostream>

using namespace std;



int main()
{
	//Program 1
    cout << "Good Morning, Planet Earth!" << endl;

	//Program 3
	int height;
	cout << "What is your height in feet? ";
    cin >> height;
	int feet_converted_inches = (height * 12) ;
	cout << "Your height in inches is " << feet_converted_inches << endl;

	//Program 4
    int weight;
	cout << "Whats your weight in pounds? ";
	cin >> weight;
	double weight_to_stone = (0.0714 * weight);
	cout << "Your weight in stone is " << weight_to_stone << endl; 

	//Program 5
	int lb;
	cout << "What is your weight in pounds? " ;
	cin >> lb;
	double weight_of_lb_of_troy = (lb * 14.583);
	double final_weight = weight_of_lb_of_troy*1000;
	cout << "Your are worth US $ " << final_weight  << "in gold!" << endl;

	//Program 6
	double  first,
	    	middle,
	    	last;

	cout << "Whats your first name: ";
	cin >> first;


        cout << "Whats your middle initial: ";
	cin >> middle;

	cout << "Whats your last name: ";
	cin >> last;


	cout << last << " , " << first  << " " << middle << endl; 


	return 0;
}
Last edited on
1
2
3
double  first;
cout << "Whats your first name: ";
cin >> first;
What are you doing here? Remember: double is a number and cannot hold anything else.

For 5: give example of input, output and desired output
Last edited on
It worked when I added some code... does it look correct ?

//Program 6

char first[20];
cin.getline(first, 20);
char middle[20];
cin.getline(middle, 20);
char last[20];
cin.getline(last, 20);

cout << "Whats your first name: ";
cin >> first;


cout << "Whats your middle initial: ";
cin >> middle;

cout << "Whats your last name: ";
cin >> last;


cout << last << " , " << first << " " << middle << endl;
But I cant make Program 5 work properly ..... I need suggestions how to make it work .... It outputs a final answer 2.04162e+006

for example when I put 140 it out puts 2.04162e+006.
and should be 2041620
Well, better to use std:string, but your way will work too.

What problems do you have with 5?
well the problem with 5 is that the answer to "final_weight" it does it calculations wrong or something because it comes out a very odd number like
"2.04162e+006"
it is scientific notation. Actually it is 2041620 or close.
#include <iomanip> and then use std::cout << std::fixed before outputting result to see fixed point notation.
http://en.cppreference.com/w/cpp/io/manip/fixed
Last edited on
Know I can see what goes on. Thank you I appreciated!
Last edited on
Topic archived. No new replies allowed.