Feb 25, 2016 at 2:57pm Feb 25, 2016 at 2:57pm UTC
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
#include "stdafx.h"
#include <iostream>
#include <string>
int main(){
int piasters;
int pounds;
std::cout << "Convert pounds to piasters or vice versa, do you want pounds to piasters or piasters to pounds?" ;
std::string ans;
std::cin >> ans;
if (ans == "Pounds to piasters" ) {
std::cout << "Please enter the amount!" ;
std::cin >> pounds;
piasters = 60 * pounds;
std::cout << pounds << "pounds" << "=" << piasters << "piasters" ;
}
else if (ans == "Piasters to pounds" ); {
std::cout << "Please enter the amount!" ;
std::cin >> piasters;
pounds = piasters / 60;
std::cout << piasters << "piasters" << "=" << pounds << "pounds" ;
}
}
What's wrong with this code?
Last edited on Feb 25, 2016 at 6:38pm Feb 25, 2016 at 6:38pm UTC
Feb 25, 2016 at 3:05pm Feb 25, 2016 at 3:05pm UTC
Please use code tags for your code to make it readable -
http://www.cplusplus.com/articles/jEywvCM9/
Your problem is that non of your if statements will ever come true. The reason being that
std::cin >>
uses white-space as seperator. Meaning it will stop reading once it finds a space. So it can never read "Pounds to piasters". You want to use std::getline -
I would also like to mention, having the user typing "Pounds to piasters" or "Piasters to pounds"
is kinda absurd. Because lowercase and uppercase letters matter. Perhaps make a menu with the numbers 1 and 2, or a and b.
Edit:
else if (ans == "Piasters to pounds" ); // remove the semicolon.
Last edited on Feb 25, 2016 at 3:08pm Feb 25, 2016 at 3:08pm UTC
Feb 25, 2016 at 3:17pm Feb 25, 2016 at 3:17pm UTC
Thanks! Is there any way to make upper and lower cases not matter? For example 'A' is viewed the same as 'a'?
Feb 25, 2016 at 3:47pm Feb 25, 2016 at 3:47pm UTC
The one way I can think of is, Just make everything the user enters into uppercase/lowercase. For example:
1 2
std::string name;
cin >> name;
Let's say user enters "Tarik".
Transform it to "tarik"
and have your if statement be
if (name == "tarik" ) {//code }
this way, no matter what the user enters, it will all be lowercase. Hope that makes sense =)
Here you can see how it is done with uppercase -
Same with with lower -
Last edited on Feb 25, 2016 at 5:08pm Feb 25, 2016 at 5:08pm UTC