Click to See Complete Forum and Search --> : A request
Tech^salvager
08-01-2004, 12:22 AM
With my caluclator program at version 0.1.4 I need Ideas of how to fix it up to run better or be more usefull. So If you have idea toss it this way.
Also I was looking at some info on C++ and saw something about "using namespace std" is that only used for begginers or is not usefull, or is this something that works better then that that most people use?
Thanks for the help and Ideas
Tech^
nothing
08-01-2004, 12:42 AM
Ask the user to input two numbers and validate the input (are they really numbers?)
Ask the user what kind of operation he wants to perform on those two numbers
Show the result and ask if the user wants to use the calculator again
I don't know man lol, just a few dumb ideas. It is really up to you to know what you want your program to do. Use your imagination and then make it happen.
About the using namespace std thing, I see it this way: when you type using namespace std right after the "includes", it is like saying that you'll use all the functions and whatever contained in those includes. I read somewhere that it might also cause conflicts but I didn't really understand what they were talking about. There are two ways to avoid using namespace std.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
int main()
{
string name;
cout << "Enter your name: ";
cin >> name;
cout << "Hello, " << name << endl;
return 0;
}
This means that you'll only use cout, cin, endl and string in your program. You could do it like this too and it is going to be the same thing. I like this way better though
#include <iostream>
#include <string>
int main()
{
std::string name;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Hello, " << name << std::endl;
return 0;
}
You get it? I'm sure someone can explain this better than me.
:t
Tech^salvager
08-01-2004, 01:25 AM
Originally posted by nothing
Ask the user to input two numbers and validate the input (are they really numbers?)
Ask the user what kind of operation he wants to perform on those two numbers
Show the result and ask if the user wants to use the calculator again
You get it? I'm sure someone can explain this better than me.
:t
Check on your third idea just need to word it better and make it use yes instead of 1
Thank you for eplaining that in terms I understand. I am a real newbie at this but I feel that by actually creating stuff then just reading how something works helps me understand how to code better. Thanks for your input Nothing
Tech^salvager
08-01-2004, 08:26 PM
If any one else has any Ideas go ahead and post them.
Tech^salvager
08-02-2004, 06:19 PM
Ok can somone help me with Validation of the numbers. I think you would somehow use the if statements. If someone could give me a hint that would be nice, not the anwser but just a hint.
Thanks for your help I appreciate it.
Tech^
nothing
08-02-2004, 09:00 PM
I always read the numbers as strings using the getline function. Then I check the string length
using the length function and if it is not greater than 0, it means that we don't have a valid input. If the string lenght is greater than 0, I use the isdigit function to validate the number(s).
;)
ScaryBinary
08-02-2004, 10:17 PM
How about a "memory" feature, where the user could store a result and retrieve it later?
Tech^salvager
08-02-2004, 11:11 PM
Ok well I will get to work on that.
Tech^salvager
08-02-2004, 11:25 PM
Originally posted by ScaryBinary
How about a "memory" feature, where the user could store a result and retrieve it later?
I was thinking of that but that will come later after I learn more.:t
Ok I am now reading up about these "Additional string operations". Thanks for pointing that out to me nothing.:t
Tech^salvager
09-14-2004, 11:14 AM
hehe I been working on my code a little nit and heres what I have so far
#include <iostream>
#include <string>
//Version 0.0.32 of caluclator program
//Made by Justin R. Barrera
using namespace std;
int main()
{
int d=0;
string restart;
while (d != restart) //Loop
{
int c=0;
int b=0;
int m=0;
cout << "Please type in a number then press enter " << endl;
cin >> c;
cout << "Please type in a number then press enter " << endl;
cin >> b;
{
cout << "Choose a mathmatical operation" << endl;
cout << "1=Multiplication" << endl;
cout << "2=Division" << endl;
cout << "3=Subtraction" << endl;
cout << "4=Addition" << endl;
cin >> m;
if (m == 1) {
cout << "The product is " << c * b << endl;
}
if (m == 2) {
cout << "The quotient is " << c / b << endl;
}
if (m == 3) {
cout << "The Difference is " << c - b << endl;
}
if (m == 4) {
cout << "The sum is " << c + b << endl;
}
}
cout << "To exit enter 0" << endl;
cin >> restart >> endl;
}
return 0;
}
now I am getting errors because I am trying to use a string with the wehile loop, I was wondering where I am going wrong at guys?
Thansk for your help.
Tech^
fishybawb
09-14-2004, 11:40 AM
You don't need a string in the while loop because you're using numbers to determine what action to take. If you made "restart" an int, then in the loop you'd have while(restart != 0) - you're trying to compare a string to an int at the minute which is a no-no.
Take a look at the switch construct too. Those successive if statements could be replaced with something like:
switch(m)
{
case 1:
// product code here
break;
case 2:
// quotient code here
etc...
}
It's generally accepted to be better code, although in practice the compiler is likely to reduce it to a series of if statements anyway. Sorry, I'm waffling now :)
Tech^salvager
09-14-2004, 11:59 AM
Its just I don't know how to use case statements yet. If you have a website I can see to learn it can you put on here?
Thanks Tech^
Tech^salvager
09-14-2004, 09:32 PM
Ok I redid my code using case statements and now I am only get one error. I am trying to figure out where it is though.
#include <iostream>
//Version 0.0.32 of caluclator program
//Made by Justin R. Barrera
using namespace std;
int main()
{
int restart=0;
while (restart != 1) //Loop
{
int c=0;
int b=0;
int m=0;
cout << "Please type in a number then press enter " << endl;
cin >> c;
cout << "Please type in a number then press enter " << endl;
cin >> b;
{
cout << "Choose a mathmatical operation" << endl;
cout << "1=Multiplication" << endl;
cout << "2=Division" << endl;
cout << "3=Subtraction" << endl;
cout << "4=Addition" << endl;
switch(m)
{
case 1:
cout << "The product is " << c * b << endl;
break;
case 2:
cout << "The quotient is " << c / b << endl;
break;
case 3:
cout << "The Difference is " << c - b << endl;
break;
case 4:
cout << "The sum is " << c + b << endl;
break;
}
cout << "To exit enter 0" << endl;
cin >> restart;
}
return 0;
}
Thanks
Tech^
Tech^salvager
09-14-2004, 09:36 PM
Welll I found the error. Thiis time my program runs but It messes up at the switch and if statements at the bottom
heres the new code
#include <iostream>
//Version 0.0.32 of caluclator program
//Made by Justin R. Barrera
using namespace std;
int main()
{
int restart=0;
while (restart != 1) //Loop
{
int c=0;
int b=0;
int m=0;
cout << "Please type in a number then press enter " << endl;
cin >> c;
cout << "Please type in a number then press enter " << endl;
cin >> b;
cout << "Choose a mathmatical operation" << endl;
cout << "1=Multiplication" << endl;
cout << "2=Division" << endl;
cout << "3=Subtraction" << endl;
cout << "4=Addition" << endl;
switch(m)
{
case 1:
cout << "The product is " << c * b << endl;
break;
case 2:
cout << "The quotient is " << c / b << endl;
break;
case 3:
cout << "The Difference is " << c - b << endl;
break;
case 4:
cout << "The sum is " << c + b << endl;
break;
}
cout << "To exit enter 0" << endl;
cin >> restart;
}
return 0;
}
Thanks
Tech^
ScaryBinary
09-14-2004, 10:12 PM
You have a few errors.
(1) You forgot to read in "m". Mod your code to something like this:
cout << "Choose a mathmatical operation" << endl;
cout << "1=Multiplication" << endl;
cout << "2=Division" << endl;
cout << "3=Subtraction" << endl;
cout << "4=Addition" << endl;
/* You forgot this! */
cin >> m;
switch(m)
{
...yadda yadda yadda...
}
(2) Your while loop will run as long as restart != 1. ...but you ask the user to enter 0 to quit. So you enter zero, which is of course not equal to 1, so your loop continues. Change your initial condition and while loop to this:
int restart=1;
while (restart != 0) //Loop
You should be good to go after that, though I'd suggest adding a "default" case to your switch statement (add after your "case 4" statement):
default:
cout << "Not a valid entry, you dope." << endl;
break;
ScaryBinary
09-14-2004, 10:16 PM
Sorry, forgot to explain that the "default" case will handle a situation where the user enters something other than 1 through 4. You can put a more suitable error message there, if you like. :)
You have to remember that users are stupid, stupid people who will break your program however they can (I know, I'm a user). Give 'em two seconds and they'll be typing in "two" instead of "2". :D
Tech^salvager
09-14-2004, 10:40 PM
Thanks Scary binary.
I try to make sure that it won't mess up but that makes for the ever lasting computer programming loop. :p
SysOpt.com
Copyright Internet.com Inc. All Rights Reserved.