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?
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.
#include <iostream>
#include <cmath>
usingnamespace 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.