Help on if then, else if...

Hello,
I am new here and I just started C++, I am trying to do if statements.
I wrote a program that calculates my profits on my investments of gold and silver but I want the program to ask me at first whether I want to calculate, gold, silver or both. I assume use strings but everytime I try make an if statement, it goes wrong. How would I do that?

I can post the code if that helps,
thanks a lot.
If you're using char arrays or char*s, then you need to use strcmp or strncmp to compare strings. Otherwise, you'd be comparing pointer values instead of the actual contents of the string.

If that's what you're doing, it would be easier to use std::strings available from the <string> header.

I can post the code if that helps

Yes, please do.
Thank you for your reply,

here is my code before attempting to have the choice in the beginning:
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
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double SilverOunce;     // How many ounces of Silver
		double SilverPrice; // Current Silver price
		double GoldOunce;   // How many ounces of Gold
		double GoldPrice;   // Current Gold price
		double GoldTotal;   // Total posesssion of Gold
		double SilverTotal; // Total posession of Silver
		double GoldProfit;  // Gold Profit part 1
		double GoldProfit2; // Gold Profit part 2
		double SilverProfit; // Silver Profit part 1
		double SilverProfit2; // Silver Profit part 2
		double GoldHist;      // Gold Price when you bought it
		double SilverHist;    // Silver Price when you bought it
		double GoldSilverT;   // Gold and Silver Total
		double GoldSilverP;   // Gold and Silver Profit
		cout << "What was the spot price of gold when you bought it?" << endl;
		cin >> GoldHist;
	cout << "\nWhat is the current spot price of gold?" << endl;
	cin >> GoldPrice;
	cout << "\nHow many troy ounces of gold do you own?" << endl;
	cin >> GoldOunce;
	cout << "\nWhat was the spot price of silver when you bought it?" << endl;
	cin >> SilverHist;
	cout << "\nWhat is the current spot price of silver?" << endl;
	cin >> SilverPrice;
	cout << "\nHow many troy ounces of silver do you own?" << endl;
	cin >> SilverOunce;
	GoldTotal = GoldPrice * GoldOunce;
	cout << "\nYou currently own $" << GoldTotal << " of gold." << endl;
	GoldProfit = GoldPrice - GoldHist;
	GoldProfit2 = GoldProfit * GoldOunce;
	cout << "\nYour total profit on your investment in gold is $" << GoldProfit2 << endl;
	SilverTotal = SilverPrice * SilverOunce;
	cout << "\nYou currently own $" << SilverTotal << " of silver." << endl;
	SilverProfit = SilverPrice - SilverHist;
	SilverProfit2 = SilverProfit * SilverOunce;
	cout << "\nYour total profit on your investment in silver is $" << SilverProfit2 << "." << endl;
	GoldSilverT = GoldTotal + SilverTotal;
	cout << "\nYou currently own $" << GoldSilverT << " in precious metals." << endl;
	GoldSilverP = GoldProfit2 + SilverProfit2;
	cout << "\nYour total profit in your investment in precious metals is $" << GoldSilverP << ".\n" << endl;
	system ("pause");

	return 0;
}


I would appreciate any help to get my started or any links to tutorials using strings with if statements. Thanks a lot for all your help guys, have a great day.
Actually, if all the user has to do is select between three options, then you could define three constants for that purpose.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
enum options {GOLD = 0, SILVER = 1, BOTH = 2};

int option = 0;

cout << "What option would you like to choose?" << endl;
cout << "(" << GOLD << "): Calculate Gold" << endl;
cout << "(" << SILVER << "): Calculate Silver" << endl;
cout << "(" << BOTH << "): Calculate Both" << endl;

cin >> option;
cin.sync();

if( option == GOLD )
{
    //calculate gold
}
else if( option == SILVER )
{
    //calculate silver
}
else if( option == BOTH )
{
    //calculate both
}

Last edited on
Thanks a lot, I will give this a try. :)
Topic archived. No new replies allowed.