problem: the cout is always strange numbers or the else cout or the cout that say "useless conversion"
. I've tried also using integers and strings instead of letters for the currencies but doesn't work anyway
I've done in Object oriented programming for necessity
#include<iostream>
using namespace std;
/*Object oriented programming*/
class Convertitore{
public:
char vEntrata, vUscita;
double qEntrata;
Convertitore();
Convertitore(char vEntrata, char vUscita, double qEntrata);
double Calcolo();
};
Convertitore::Convertitore(){
vEntrata = 'a';
vUscita = 'a';
qEntrata = 0;
}
Convertitore::Convertitore(char vEntrata, char vUscita, double qEntrata){
vEntrata = vEntrata;
vUscita = vUscita;
qEntrata = qEntrata;
}
double Convertitore::Calcolo(){
if(vEntrata == 'e' && vUscita == 'e'){
cout << "Useless conversion";
}
else if(vEntrata == 'e' && vUscita == 'd'){
cout << qEntrata << "€ equal to " << qEntrata*1.22 << "$";
}
else if(vEntrata == 'e' && vUscita == 'l'){
cout << qEntrata << "€ equal to " << qEntrata*1936 << "£";
}
else if(vEntrata == 'd' && vUscita == 'e'){
cout << qEntrata << "$ equal to " << qEntrata/1.22 << "€";
}
else if(vEntrata == 'd' && vUscita == 'd'){
cout << "Useless conversion";
}
else if(vEntrata == 'd' && vUscita == 'l'){
cout << qEntrata << "$ equal to " << qEntrata*1510.29 << "£";
}
else if(vEntrata == 'l' && vUscita == 'e'){
cout << qEntrata << "£ equal to " << qEntrata/1936.27 << "€";
}
else if(vEntrata == 'l' && vUscita == 'd'){
cout << qEntrata << "£ equal to " << qEntrata/1510.29 << "$";
}
else if(vEntrata == 'l' && vUscita == 'l'){
cout << "Useless conversion";
}
else{
cout << "wrong inputs";
}
}
int main(){
char vEntrata, vUscita;
double qEntrata;
cout << "Scegli la valuta da convertire: digita e per euro, d per dollar, l per italian lira: "; cin >>
vEntrata;
cout << "Scegli la valuta in cui convertire: digita e per euro, d per dollar, l per italian lira: "; cin
>> vUscita;
cout << "digitare la quantità di " << vEntrata << " da convertire: "; cin >> qEntrata;
Convertitore Valuta = Convertitore(vEntrata, vUscita, qEntrata);
cout << Valuta.Calcolo();
return 0;
}
In your constructor
Convertitore::Convertitore(char vEntrata, char vUscita, double qEntrata){
vEntrata = vEntrata;
vUscita = vUscita;
qEntrata = qEntrata;
}
You are assigning local variables to themselves.
You can use the member initializer list instead:
Convertitore::Convertitore(char vEntrata, char vUscita, double qEntrata)
: vEntrata(vEntrata),
vUscita(vUscita),
qEntrata(qEntrata)
{}
Secondly your function double Convertitore::Calcolo() promises to return a double, but doesn't return anything.
Because of this the line
cout << Valuta.Calcolo();
causes undefined behaviour and most likely will print garbage data.
It seems like Calcolo isn't designed to return something, but print the result directly. If that's what you want, change the return type to void:
void Convertitore::Calcolo()
and don't attempt to print the (now non-existent) return value.
Related
#include <iostream>
using namespace std;
int main()
{
string n;
cout << "Enter the name of an automobile: " << endl;
cin >> n;
while( n != "End") {
if( n == ("Tesla" or "Volt" or "Leaf")) {
cout << "Electric" << endl;}
else {
if( n == ("Clarity" or "Mirai")){
cout << "Hydrogen Powered" << endl;}
else {
cout << "Gas Powered" << endl; }}
cout << "Enter the name of an automobile: " << endl;
cin >> n;
}
return 0;
}
It needs to say how each automobile is powered. Basically if I input "Tesla", it should say "Electric"; "Ford" should come up "Gas Powered". It ends when I enter "End".
I get this error
.cpp|16|error: invalid operands to binary expression ('std::__1::string' (aka 'basic_string, allocator >') and 'bool')|
This should work! In C++ there is no or. Use ||. Also the condition needs to be put each time in if like if(n=="Tesla"||n=="Volt"||n=="Leaf").
#include <iostream>
using namespace std;
int main()
{
string n;
cout << "Enter the name of an automobile: " << endl;
cin >> n;
while( n != "End") {
if( n == "Tesla" || n== "Volt" ||n== "Leaf") {
cout << "Electric" << endl;}
else {
if( n == "Clarity" || n=="Mirai"){
cout << "Hydrogen Powered" << endl;}
else {
cout << "Gas Powered" << endl; }}
cout << "Enter the name of an automobile: " << endl;
cin >> n;
}
return 0;
}
if( n == ("Tesla" or "Volt" or "Leaf"))
needs to be changed to
if( (n == "Tesla") || (n == "Volt") || (n == "Leaf") )
Make similar changes at the other places where you do the same thing
There is no or keyword in C++. You need to use ||
("Tesla || "Volt" || "Leaf") would always return true. So the condition will become if (n == (true)) which will never be true.
The game does not want to exit/close. Somebody enlighten me as to what I've missed? Also, do you think i have coded the game properly?
Source code:
#include<iostream>
#include<iomanip>
using namespace std;
void drawBoard(char board[][3]);
char checkWinner3by3(char board[][3]);
int main()
{
char board[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
int row;
int column;
bool is_move;
bool is_row;
bool is_column;
cout<<"********** TIC TAC TOE ************\n";
{
is_move = false;
is_row = false;
is_column = false;
drawBoard(board);
cout << "Player ";
if(player == 'X')
{
cout << 1;
}
else
{
cout << 2;
}
cout << "'s turn:" << endl;
is_move = false;
while(!is_move)
{
while(!is_row)
{
cout << "Row: ";
cin >> row;
if(row == 1 || row == 2 || row == 3)
{
is_row = true;
}
else
{
cout << endl << "Invalid row!" << endl;
}
}
/
is_column = false;
while(!is_column)
{
cout << "Column: ";
cin >> column;
if(column == 1 || column == 2 || column == 3)
{
is_column = true;
}
else
{
cout << endl << "Invalid column!" << endl;
}
}
Irrelevant stuff here.
if(board[row-1][column-1] == ' ')
{
board[row-1][column-1] = player;
is_move = true;
if(player == 'X')
{
player = 'O';
}
else
{
player = 'X';
}
}
else
{
cout<<"The selected space is occupied!" << endl;
cout << "Select a different space:" << endl << endl;
drawBoard(board);
}
}
cout << endl;
Checking the winner
winner = checkWinner3by3(board);
if(winner == 'X' || winner == 'O')
{
drawBoard(board);
cout<<"Congratulations! Player ";
if(winner == 'X')
{
cout << 1;
}
else
{
cout << 2;
}
cout<<" is the winner!"<<endl;
}
else if(winner == 'T')
{
drawBoard(board);
cout << "It's a tie!" << endl;
}
}
system("pause");
return 0;
}
Board is here.
void drawBoard(char board[][3])
{
char print[][3] = {{' ',' ',' '},
{' ',' ',' '},
{' ',' ',' '}};
cout << " 1 2 3" << endl;
cout << " +---+---+---+" << endl;
cout << " 1" << " | " << print[0][0] << " | " << print[0][1] << " | "
<< print[0][2] << " | " << endl;
cout << " +---+---+---+" << endl;
cout << " 2" << " | " << print[1][0] << " | " << print[1][1] << " | "
<< print[1][2] << " | " << endl;
cout << " +---+---+---+" << endl;
cout << " 3" << " | " << print[2][0] << " | " << print[2][1] << " | "
<< print[2][2] << " | " << endl;
cout << " +---+---+---+" << endl;
}
char checkWinner3by3(char board[][3])
{
for(i=0; i<3; i++)
{
if(board[i][0]==board[i][1] && board[i][0]==board[i][2])
{
return board[i][0];
}
}
for(i=0; i<3; i++)
{
if(board[0][i]==board[1][i] && board[0][i]==board[2][i])
{
return board[0][i];
}
}
if(board[0][0]==board[1][1] && board[1][1]==board[2][2])
{
return board[0][0];
}
if(board[0][2]==board[1][1] && board[1][1]==board[2][0])
{
return board[0][2];
}
return ' ';
}
This is the entire source code - i wanted to make a game and i honestly think this is the best way to start by knowing what i can do to improve it.
You have not declared the variable "player" & "winner" yet have initialized them...but this could be because the full code is not displayed.
you have system("pause") before return... why did you put that there?
...now that I look further its all screwed up
I made a Tic-Tac-Toe once and it's kinda similar to what your trying to do apart from selection via 2d array, you should consider using parts of it to correct your code.
#include <iostream>
#include <iomanip>
using namespace std;
char* cBox{ new char[9]{ '1', '2', '3', '4', '5', '6', '7', '8', '9' } }; // cBox are tic tac toe squares
char* PcBox{ nullptr }; // PcBox will be pointers to cBox squares
bool EndGame{ true }, VailidMove{ true }, WinGame{ true }; // Global bool varibles are initially set to true to test for validity
size_t PlayerMove{ 1 };
int Move;
int score(size_t PlayerMove); /********************************/
void Board(); /* */
void error(); /* Function Prototypes */
void GameOver(); /* */
int scoreboard(int score1, int score2); /********************************/
int main() {
int score1{}, score2{};
int sum{};
int winner{};
int const total{ 756 };
char PlayerIcon;
// do while loop
do {
// print scoreboard
scoreboard(score1, score2);
// Print board
Board();
// This conditional forces the first Player to be 'X'... Player2 'O'
(PlayerMove == 1) ? PlayerIcon = 'X' : PlayerIcon = 'O';
// Time for the player to move
cout << endl << setw(51) << "Player" << PlayerMove << "'s move:";
cin >> Move;
VailidMove = false; // ValidMove is assigned to false within the "do loop"...it will remain false until a valid key is entered..
//... it's conditionally tested within the while loop until the player's move is true(valid)
// Loop until we get a valid move
while (!VailidMove) {
system("CLS"); // Clear the screen
(PcBox = &cBox[Move - 1]); // For our move selection, point PcBox to the address of the array cBox[index] on the free store
if (!(*PcBox == 'X' || *PcBox == 'O') && (cin) && Move != 0) // this checks the location for PlayerIcons...as one cannot place a new playericon where one already resides...
// ...also tests whether a legit key input..aka not a "letter" or a "0"
{
VailidMove = true;
*PcBox = PlayerIcon; // A valid playericon is placed in a free box on the board
}
else
{
error(); // Anything else other than a valid selection will throw an error and return to the beginning of the block...
return main(); // ...querying the player for a valid selection until one is entered
}
};
EndGame = false; // Again like "VailidMove" we set EndGame to false to test against the following conditions
// This is a combintion of seperate win conditions... 3 the same, across or diagonal over entire board
if (cBox[0] == cBox[1] && cBox[1] == cBox[2])
GameOver();
if (cBox[0] == cBox[4] && cBox[8] == cBox[4])
GameOver();
if (cBox[1] == cBox[4] && cBox[7] == cBox[4])
GameOver();
if (cBox[2] == cBox[4] && cBox[6] == cBox[4])
GameOver();
if (cBox[2] == cBox[8] && cBox[5] == cBox[8])
GameOver();
if (cBox[3] == cBox[0] && cBox[6] == cBox[0])
GameOver();
if (cBox[3] == cBox[4] && cBox[5] == cBox[4])
GameOver();
if (cBox[6] == cBox[8] && cBox[7] == cBox[8])
GameOver();
// If the board is full and no one wins then it is a tie...this piece just sums up all the player icons as ascii integers...
// ...if the board is full of them, then the sum is 756 and it is a tie.
sum += (*PcBox); // add player icon to sum...Ascii to int
if ((sum == total) && !EndGame) // if sum of playericon for entire board = 756...no win
{
EndGame = true;
WinGame = false;
sum = 0; //reset sum to zero
scoreboard(score1, score2);
}
// End/Win game conditions
if (EndGame) { // game over? test choices
if (WinGame) { // We nest this here soley if EndGame & WinGame is true... else if just WinGame is true its a draw
if (PlayerMove == 1) { score1 = score(PlayerMove); }
if (PlayerMove == 2) { score2 = score(PlayerMove); }
scoreboard(score1, score2);
winner = PlayerMove;
}
Board();
if (WinGame)
cout << "\n" << setw(51) << "PLAYER " << winner << " WINS!" << endl;
cout << endl << setw(57) << "Game Over!!!" << endl;
cout << endl << setw(59) << "Play again y/n?"; // Query the player for another game
char PlayAgain;
cin >> PlayAgain;
// This resets the board, if the player wants another game, it uses a for loop to increment simultaneously it's "ascii integer", along side it's "int" equivalent...
//... overwriting & reseting the board from 1-9 with the original characters
if (PlayAgain == 'y' || PlayAgain == 'Y') {
EndGame = false;
for (int i = 0, j = 49; i < 9 && j <= 57; i++, j++) // ASCII int 49 = char '1'... to 57 = char '9'
{
(cBox[i] = (char)j); // Clear the board
}
sum = 0; // Reset sum to zero
system("CLS"); // Clear the screen
}
PlayerMove = 1;
if (PlayAgain == 'n') {
EndGame = true;
}
}
else
{
(PlayerMove == 1) ? PlayerMove = 2 : PlayerMove = 1;
}
} while (!EndGame);
delete[] cBox; // Free up memory...
cBox = nullptr;
cout << "\n\t\t\t\t\t";
return 0;
}
// Function definiton to print game board
inline void Board() {
{
cout << endl << '\n' << '\n' << endl;
cout << setw(50) << cBox[0] << "|" << cBox[1] << "|" << cBox[2] << endl;
cout << setw(54) << "-+-+-" << endl;
cout << setw(50) << cBox[3] << "|" << cBox[4] << "|" << cBox[5] << endl;
cout << setw(54) << "-+-+-" << endl;
cout << setw(50) << cBox[6] << "|" << cBox[7] << "|" << cBox[8] << endl;
}
}
// Function definiton to keep score
int score(size_t PlayerMove)
{
int static score1{ 1 };
int static score2{ 1 };
if (PlayerMove % 2)
return score1++;
else
return score2++;
}
// Function definiton to print scoreboard
int scoreboard(int score1, int score2)
{
cout << endl << setfill(' ') << setw(47) << "PLAYER 1: " << score1 << " " << "PLAYER 2: " << score2;
return(score1, score2);
}
// Function definiton to throw an error
void error() {
{
cout << setfill(' ') << setw(64) << "Invalid Move. Try again." << endl;
cin.clear(); //clear the error flags.
cin.ignore(); //sync up the stream in case the user entered white space
}
}
// Function definiton to end game
void GameOver()
{
EndGame = true;
}
I am currently working on a program for a project, that asks for the user to enter the specific sport they want to play and their age for reservations at a recreation area. I am confused on how to store their sport and age into an array so that it can be displayed later in the program, if they select to view all reservations made by one or more users. If anyone could help me with figuring out how to store a single or multiple user input into an array so that it can be displayed later in the program that would be great!
#include <iostream>
#include <string>
using namespace std;
int main()
{
char t; // Type of sport selected
char g, G; // Gliding
char h, H; // Hang-gliding
char f, F; //Flying
int a; // Age of patron
double x; // Rates
int s; // Selection from menu
int i; // Arrays variable
int num;
char sport[100]; // Array for all sports of patrons
int age[100]; // Array for all ages of patrons
cout << "Please pick from the following menu" << endl;
cout << "1. Add a new reservation" << endl;
cout << "2. Print all reservations" << endl;
cout << "3. Print all reservations for a given sport" << endl;
cout << "4. Quit" << endl;
cin >> s;
for (i = 0; i < num; ++i)
if (s == 1) {
cout << "Please enter f/F for flying, g/G for gliding and h/H for hang-gliding" << endl;
cin >> t;
getline (cin, sport[i]);
cout << "Please enter the age of patron, minimum age is 16" << endl;
cin >> a;
if ((t == 'f' || t == 'F') && (a <= 25)) {
x = 68.95;
}
else if ((t == 'g' || t == 'G') && (a <= 25)) {
x = 73.95;
}
else if ((t == 'h' || t == 'H') && (a <= 25)) {
x = 99.95;
}
else if ((t == 'f' || t == 'F') && (a > 25)) {
x = 55.95;
}
else if ((t == 'g' || t == 'G') && (a > 25)) {
x = 65.95;
}
else if ((t == 'h' || t == 'H') && (a > 25)) {
x = 92.95;
}
cout << "The insurance rate is $ " << x << endl;
}
else if (s == 2) {
cout << "A patron aged " << a << " reserved a session of " << t << endl;
}
else if (s == 3) {
}
else if (s == 4);
return 0;
You should make a class Patron that contains the multiple informations, then make an array of type Patron instead of multiple arrays:
class Patron
{
//data for each patron...
};
in main:
Patron patrons[...];
You could also use dynamic containers, like vector instead of an array.
std::vector<Patron> patrons;
I am writing a "time travel" program which is supposed to ask the user for the current time, their target travel time, relay those values to be converted into minutes in a function and then based on if the time difference is too high it will not allow the time travel or if it is allowed it will print if they are in the future or the past. My issue right now is that the current time, which is only supposed to be relevant the first iteration, or the first "jump" of the program is not being updated after the jump occurs, and the program defaults to it instead of the target time, which is supposed to be the technical "current time" after the jump has occurred. I have been trying to figure this out for hours and I can't, so I am hoping someone could help me out.
Thank you for your time
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int compute_time_difference(int c_hrs, int c_mins, bool c_am, int t_hrs, int t_mins, bool t_am);
void print_future();
void print_past();
void print_SecurityProtocol();
int main()
{
int c_hrs, c_mins;
int t_hrs, t_mins;
int system_time2;
int time_difference2;
bool c_am = 0;
bool t_am = 0;
char c, c2, Y, N, y, n, jumpAgain;
string am_or_pm_current, am_or_pm_target;
bool g_stop = true;
cout << "Welcome to Po Sled" << endl;
cout << "\tSystem booting..." << endl;
for (int i = 0; i<1; i++) //for loop to run once
{
cout << "\n\tEnter current time below: " << endl;
cout << "\t>Enter hour: "; //User inputs current time in hours
cin >> c_hrs;
while (c_hrs > 12 || c_hrs < 1)
{
cout << "Error: Please enter an hour in the range [1, 12]: ";
cin >> c_hrs;
}
cout << "\t>Enter minutes: "; //User inputs current time in minutes
cin >> c_mins;
while (c_mins > 59 || c_mins < 0) {
cout << "Error: Please enter minutes in the range [0, 59]: ";
cin >> c_mins;
}
cout << "\t>Is it AM or PM?: "; //Classifying if current time is AM or PM
cin >> am_or_pm_current;
while (am_or_pm_current != "am" && am_or_pm_current != "AM" && am_or_pm_current != "pm" && am_or_pm_current != "PM") { //Checks if valid input, if not repeats message until valid
cout << "\tError: Please enter AM/am or PM/pm: ";
cin >> am_or_pm_current;
}
if ((am_or_pm_current == "am") || (am_or_pm_current == "AM"))
{
c_am = 1;
}
else if ((am_or_pm_current == "pm") || (am_or_pm_current == "PM"))
{
c_am = 0;
}
cout << "\n\tCurrent system time set to " << c_hrs << ":" << setw(2) << setfill('0') << c_mins << am_or_pm_current << endl;
cout << "\n\t\tIs this time correct (Y or N)? ";
cin >> c;
while (c != 'Y' && c != 'y' && c != 'n' && c != 'N')
{
cout << "\t\t\tError: Please enter Y/y or N/n: ";
cin >> c;
}
if (c == 'N' || c == 'n')
{
continue;
}
else
{
cout << "\n\tSystem initializing and TARDIS unit warming...." << endl;
cout << "\tThe Po Sled is engaged and ready for input." << endl;
}
}
do {
//Starts a loop for target jump
cout << "\n\tEnter target time below: " << endl; //Enters target time of jump
cout << "\t>Enter Hour: ";
cin >> t_hrs;
while (t_hrs > 12 || t_hrs < 1) {
cout << "Error: Please enter an hour in the range [1, 12]: ";
cin >> t_hrs;
}
cout << "\t>Enter minutes: ";
cin >> t_mins;
while (t_mins > 59 || t_mins < 0) {
cout << "\tError: Please enter minutes in the range [0, 59]: ";
cin >> t_mins;
}
cout << "\n\tIs it AM or PM?: ";
cin >> am_or_pm_target; //Classifying if target time is AM or PM
while (am_or_pm_current != "am" && am_or_pm_current != "AM" && am_or_pm_current != "pm" && am_or_pm_current != "PM")
{ //Validates input is AM or PM
cout << "\tError: Please enter AM/am or PM/pm: ";
cin >> am_or_pm_target;
}
if ((am_or_pm_target == "am") || (am_or_pm_target == "AM"))
{
t_am = 1;
}
else if ((am_or_pm_target == "pm") || (am_or_pm_target == "PM"))
{
t_am = 0;
}
cout << "\tTarget time set to " << t_hrs << ":" << setw(2) << setfill('0') << t_mins << am_or_pm_target; //Sets target time
cout << "\n\t\tIs this time correct (Y or N)? "; //Validates if target time entered is correct
cin >> c2;
while (c2 != 'Y' && c2 != 'y' && c2 != 'n' && c2 != 'N')
{
cout << "\t\t\tError: Please enter Y/y or N/n: ";
cin >> c2;
}
time_difference2 = compute_time_difference(c_hrs, c_mins, c_am, t_hrs, t_mins, t_am);
if (time_difference2 > 360) //If time difference is greater than 6 hours prints error function
{
print_SecurityProtocol();
continue;
}
if (c2 == 'N' || c2 == 'n')
{
continue;
}
cout << "\tJump was made, the current time is " << t_hrs << ":" << setw(2) << setfill('0') << t_mins << am_or_pm_target << endl;
if (time_difference2 < 0 && time_difference2 > -360) //If time difference is less than 0 prints past function
{
print_past();
}
else if (time_difference2 >= 0 && time_difference2 <= 360) //If time difference is ahead of current time prints future function
{
print_future();
}
cout << "\tWould you like to jump again (Y/N)? ";
cin >> jumpAgain;
while (jumpAgain != 'Y' && jumpAgain != 'y' && jumpAgain != 'n' && jumpAgain != 'N') //Input validation
{
cout << "\t\t\tError: Please enter Y/y or N/n: ";
cin >> jumpAgain;
}
if (jumpAgain == 'n' || jumpAgain == 'N') //User exiting program
{
if (time_difference2 < 0)
{
cout << "\t\tSystem shutting down; enjoy the past.\n" << endl;
}
else if (time_difference2 >= 0 && time_difference2 < 360)
{
cout << "\t\tSystem shutting down; enjoy the future.\n" << endl;
}
}
if (jumpAgain == 'Y' || jumpAgain == 'y')
{
continue;
}
} while (jumpAgain != 'n' && jumpAgain != 'N');
return 0;
}
int compute_time_difference(int c_hrs, int c_mins, bool c_am, int t_hrs, int t_mins, bool t_am) //Computes time differences.
{
int currentTime_hours, currentTime_minutes, targetTime_hours, targetTime_minutes, currentTime, targetTime;
int time_difference;
if (c_am == 1) //if c_am is true and it is morning
{
if (c_hrs == 12)
{
currentTime_hours = c_hrs * 0;
}
else if (c_hrs != 12)
{
currentTime_hours = (c_hrs * 60);
currentTime_minutes = c_mins;
currentTime = currentTime_hours + currentTime_minutes; //Sets the value of the current time
}
}
else if (c_am == 0) //if c_am is false and it is afternoon time
{
if (currentTime_hours == 12)
{
c_hrs = c_hrs * 60;
}
else if (currentTime_hours != 12)
{
currentTime_hours = ((c_hrs + 12) * 60);
currentTime_minutes = c_mins;
currentTime = currentTime_hours + currentTime_minutes; //Sets the value of the current time
}
}
if (t_am == 1) //if t_am is true and it is morning time
{
if (targetTime_hours == 12) //if target hours equal to 12 special math
{
targetTime_hours = t_hrs*0;
}
else if (targetTime_hours != 12) //else do this math
{
targetTime_hours = ((t_hrs) * 60);
targetTime_minutes = t_mins;
targetTime = targetTime_hours + targetTime_minutes;
}
}
else if (t_am == 0) //if target time equal to pm then do this math
{
if (targetTime_hours == 12)
{
targetTime_hours = t_hrs * 60;
}
else if (targetTime_hours != 12) //else if target time not equal to 12 then do normal pm math
{
targetTime_hours = ((t_hrs + 12) * 60);
targetTime_minutes = t_mins;
targetTime = targetTime_hours + targetTime_minutes;
}
}
time_difference = targetTime - currentTime;
cout << "the difference computed is " << time_difference;
return time_difference;
}
void print_SecurityProtocol() //Function which prints security protocol error message
{
cout << "\tSecurity Protocols Engaging" << endl;
cout << "\t\tError: The time difference is greater than 6 hours." << endl;
cout << "\t\t\tPlease re-enter target time." << endl;
}
void print_past() //Function that prints when a user is in the past
{
cout << "\tHold onto your lower posterior regions" << endl;
cout << "\n\t\tYou are now in the relative past" << endl;
}
void print_future() //Function that prints when a user is in the future
{
cout << "\tHold onto your lower posterior regions" << endl;
cout << "\n\t\tYou are now in the relative future " << endl;
}
Why do you not use a system time call, instead of getting user input for the 'current time'? Also, use a 24-hour clock instead of a 12-hour and you have removed the need to cater for am/pm within your process.
AM n ot much of a coder but c_hrs*60 would be (at least) 60 hours and (at most) 24*60 which, with the exception of a drive across Europe, Russia, Australia or the Northern US would be excessive time recording. Do you not mean to refer to MINUTES instead of hours (c_mins*60)?
Hello I'm trying to create a console application that allows the user to input a single character to preform an arithmetic operation.
Currently the program only adds the two numbers together even if I input an m, meaning multiply. I believe it is going straight into the first if statement for some reason even if I dont want addition.
#include <iostream>
using namespace std;
int add(int a, int b)
{
int c;
c = a + b;
return c;
}
int subtract(int a, int b)
{
int c;
c = a - b;
return c;
}
int multiply(int a, int b)
{
int c;
c = a * b;
return c;
}
int divide(int a, int b)
{
int c;
c = a / b;
return c;
}
int remainder(int a, int b)
{
int c;
c = a % b;
return c;
}
int main ()
{
int a;
int b;
char op;
cout << "****************Integer Calculator**************** " << endl << "Press enter to continue." << endl;
cout << "Enter the first integer: " << endl;
cin >> a;
cout << "Enter the second integer: " << endl;
cin >> b;
cout << "What operation would you like to perform? Enter a single character " << endl << "Add - A , a or + " << endl <<
"Subtract - S , s or - " << endl << "Multiply - M , m or * " << endl << "Divide - D , d or / " << endl << "Remainder - R , r or % " << endl;
cin >> op;
if (op ='A' || 'a' || '+')
{
int answer1 = 0;
answer1 = add(a, b);
cout << "The answer is: " << answer1 << endl;
system("PAUSE");
return 0;
}
else if(op == 'S' || 's' || '-')
{
int answer2 = 0;
answer2 = subtract(a, b);
cout << "The answer is: " << answer2 << endl;
system("PAUSE");
return 0;
}
else if (op == 'M' || 'm' || '*')
{
int answer3 = 0;
answer3 = multiply(a, b);
cout << "The answer is: " << answer3 << endl;
system("PAUSE");
return 0;
}
else if (op == 'D' || 'd' || '/')
{
int answer4 = 0;
answer4 = divide(a, b);
cout << "The answer is: " << answer4 << endl;
system("PAUSE");
return 0;
}
else if (op == 'R' || 'r' || '%')
{
int answer5 = 0;
answer5 = remainder(a, b);
cout << "The answer is: " << answer5 << endl;
system("PAUSE");
return 0;
}
Your logic is incorrect, and you are using the assignment operator to boot.
if (op ='A' || 'a' || '+')
This should be:
if (op == 'A' || op == 'a' || op == '+')
Usually for stuff like this we use a switch:
switch( toupper(op) )
{
case 'A':
case '+':
// Do adding...
break;
case 'S':
case '-':
// Do subtraction...
break;
// etc...
default:
cout << "Unknown operation : " << op << endl;
}
In your if statement
(op ='A' || 'a' || '+')
you are not testing if op equals 'A'. You are setting op equal to 'A', and then testing if 'a' and '+' are true, which they are. To fix this, use the == operator, and test op for every case like so:
if (op == 'A' || op == 'a' || op == '+')...
else if(op == 'S' || op == 's' || op == '-')...
// and so on