Reputation: 1
This is my final project for the semester. When I run it on replit, the program runs but doesn't work fully after the first step. When I run it on VS Code, it gives me a linker command error. This is due in 3 days, please any help is appreciated. Thank you
Below is my code:
`
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
const int HEAD_SPACE = 23;
const int SECND_HDSPACE = 35;
const int THRD_HDSPACE = 27;
char startKey;
char replayKey;
int userChoice;
int main()
{
float conversion(void);
firstStart:
cout << "=======================================================\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\t ::: CURRENCY CONVERTER :::\n\n";
cout << "=======================================================\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\tPlease select any options below\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\t1) Press [1] to convert from USD to GBP\n";
cout << setw(SECND_HDSPACE) << "\t\t\t2) Press [2] to convert from USD to EUR\n";
cout << setw(SECND_HDSPACE) << "\t\t\t3) Press [3] to convert from USD to NZD\n";
cout << setw(SECND_HDSPACE) << "\t\t\t4) Press [4] to convert from USD to JPY\n";
cout << setw(SECND_HDSPACE) << "\t\t\n\n :: PRESS [S] TO START PROGRAM :: \n\n";
menuOption:
cin >> startKey;
if(startKey == 's' && startKey == 'S')
{
float resultVal = conversion();
cout << "The conversion is: " << resultVal << endl;
cout << "Would you like to run the program again? Press Y or N" << endl;
scndStrt:
cin >> replayKey;
if (replayKey == 'y' && replayKey == 'Y')
{
goto firstStart;
}
else if ((replayKey == 'n' && replayKey == 'N'))
{
cout << "Thanks for using my program!" << endl;
}
else
{
cout << "Wrong Key Entered. Please hit Y or N" << endl;
goto scndStrt;
}
}
else
{
cout << "Wrong Key Entered. Please Hit [S]";
goto menuOption;
}
float convervion(void);
int selection;
int currChoice;
float currency1;
float currency2;
cout << "Please enter the currency name: " << endl;
cin >> selection;
cout << "Please enter the amount you would like to convert: " << endl;
cin >> currency1;
switch (selection)
{
case '1':
formulaInput:
cout << "Please enter what currency you would like to conver in: " << endl;
cin >> currency2;
if (currency2 == '1')
{
currency2 = currency1 * 1;
}
else if (currency2 == '2')
{
currency2 = currency1 * 0.84;
}
else if (currency2 == '3')
{
currency2 = currency1 * 0.94;
}
else if (currency2 == '4')
{
currency2 = currency1 * 1.92;
}
else
{
cout << "Incorrect input, please try again." << endl;
goto formulaInput;
}
}
}
`
I tried to watch some videos but the terminology and syntax seemed too advanced for me. I'm only a first year CS student and this is my first semester.
Upvotes: 0
Views: 76
Reputation: 71
First of all, this program will not run correctly. But beside of that and to your question:
You have to close the main and open the conversion function(s). Check all your curly braces to be correctly.
Your function declaration is inside the main function. As retired Ninja in the comment section said, it is not incorrect and you can do this. Please think about local and global scope. (Within the main function it will become local scope for main and outside the declaration would be in global scope)
But if you think about it, if you wold define another function beneath the in local scope declared function, the same function will become global scope anyway. So for this reason it would be preferable to declare the function in global scope directly. For now, just from the standpoint, to avoid unnecessary irritation.
You have a misspell in your functions declaration and definition. (conversion / convervion ) please check this.
Your function is declared and defined with a return value of float but you forget the final return statement, which you will need to print out the calculation result.
.
#include <iostream> // Do you realy need all of these includes?
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
const int HEAD_SPACE = 23;
const int SECND_HDSPACE = 35;
const int THRD_HDSPACE = 27;
char startKey;
char replayKey;
int userChoice;
float conversion(void);
int main()
{
firstStart:
cout << "=======================================================\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\t ::: CURRENCY CONVERTER :::\n\n";
cout << "=======================================================\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\tPlease select any options below\n\n";
cout << setw(SECND_HDSPACE) << "\t\t\t1) Press [1] to convert from USD to GBP\n";
cout << setw(SECND_HDSPACE) << "\t\t\t2) Press [2] to convert from USD to EUR\n";
cout << setw(SECND_HDSPACE) << "\t\t\t3) Press [3] to convert from USD to NZD\n";
cout << setw(SECND_HDSPACE) << "\t\t\t4) Press [4] to convert from USD to JPY\n";
cout << setw(SECND_HDSPACE) << "\t\t\n\n :: PRESS [S] TO START PROGRAM :: \n\n";
menuOption:
cin >> startKey;
if (startKey == 's' && startKey == 'S')
{
float resultVal = conversion();
cout << "The conversion is: " << resultVal << endl;
cout << "Would you like to run the program again? Press Y or N" << endl;
scndStrt:
cin >> replayKey;
if (replayKey == 'y' && replayKey == 'Y')
{
goto firstStart;
}
else if ((replayKey == 'n' && replayKey == 'N'))
{
cout << "Thanks for using my program!" << endl;
}
else
{
cout << "Wrong Key Entered. Please hit Y or N" << endl;
goto scndStrt;
}
}
else
{
cout << "Wrong Key Entered. Please Hit [S]";
goto menuOption;
}
}
float conversion()
{
int selection;
int currChoice;
float currency1;
float currency2;
cout << "Please enter the currency name: " << endl;
cin >> selection;
cout << "Please enter the amount you would like to convert: " << endl;
cin >> currency1;
switch (selection) // ??
{
case '1':
formulaInput:
cout << "Please enter what currency you would like to conver in: " << endl;
cin >> currency2; // This is not working.
if (currency2 == '1')
{
currency2 = currency1 * 1;
}
else if (currency2 == '2')
{
currency2 = currency1 * 0.84;
}
else if (currency2 == '3')
{
currency2 = currency1 * 0.94;
}
else if (currency2 == '4')
{
currency2 = currency1 * 1.92;
}
else
{
cout << "Incorrect input, please try again." << endl;
goto formulaInput;
}
}
// Here you have to return something?
return currency2;
}
Upvotes: 3