Click to See Complete Forum and Search --> : Calculator program problem
Tech^salvager
07-29-2004, 09:43 PM
Hi all. Well I have a problem with my source code in my caluclator program I am writting up. I was wondering if anybody could give me a little help. the problem is I don't know how to make the number inputed by the user multiply together. So I thought I would come here to see if I could get some help, I will give trying to find a solution for it by looking through my book.
Thanks to all
Tech^:t
And this time I am writtingg it up in C++.
#include <iostream>
using namespace std;
int main()
{
int a=0;
int b=0;
int c=0;
cout << "This program multiplys two numbers inputed by the user" << endl;
cout << endl << endl;
cout << "Please input a number." << endl;
cin >> a;
cout << "Please input a number." << endl;
cin >> b;
cout << "The numbers you enter equal" << endl;
cout << a*b=c << endl;
cout << "Thank you for using this program" << endl;
return 0;
}
nothing
07-29-2004, 10:37 PM
You don't need that third variable. Just use
cout << a * b;
If you want to assig a * b to c, do it like this:
c = a * b;
Hope that helps ;)
Tech^salvager
07-29-2004, 11:38 PM
Thanks nothing the first ne worked though you are correct about using the second one. Now I will have to put a loop into the program so It either quit or run again, now I will have to learn about that.
Tech^salvager
07-29-2004, 11:40 PM
Originally posted by nothing
You don't need that third variable. Just use
cout << a * b;
Hope that helps ;)
So it does matter about the spacing durng mathmatical operations?
fishybawb
07-30-2004, 05:13 AM
The spacing won't matter in any of your source code - the compiler ignores "whitespace" ie. spaces, extra carriage returns/linefeeds and tabs. If you wanted to, you could do:
cout << a * b
Whatever you find most readable is fine.
Tech^salvager
07-30-2004, 06:08 AM
Thanks fishybawb
Tech^salvager
07-30-2004, 06:50 AM
Well I am trying to put a loop into it I believe the sentinel controllerd loop. Well I have a bit of problem finding the problem, can some one give me hint as to where I am going wrong or what I am not doing correcrtly.
Thanks for your help
Tech^:t
#include <iostream>
using namespace std;
int main()
{
cin >> c;
while (c == 0);
(
int a=0;
int b=0;
int c=0;
cout << "This program multiplys two numbers inputed by the user" << endl;
cout << endl << endl;
cout << "Please input a number." << endl;
cin >> a;
cout << "Please input a number." << endl;
cin >> b;
cout << "The numbers you enter equal";
cout << a * b << endl;
cout << "Thank you for using this program" << endl;
cout << "Would you like two continue using this program";
cout << " if so press any key except zero if you want to";
cout << " quit the press zero." << endl;
cin >> c;
return 0;
)
}
fishybawb
07-30-2004, 06:59 AM
Try while(c != 0) ie. perform the loop when c is not equal to zero. You also haven't declared the variable "c" until inside the while loop, but you're trying to use it before - you have to define a variable before you use it. Replace the first cin >> c line with int c = 1. That would satisfy the != 0 part of the while loop and ensure at least one run through it, and declare the variable at the same time. You can then remove the int c = 0 line inside the while loop.
Tech^salvager
07-30-2004, 01:55 PM
Well did as you said and It is still returning some errors.
seems my error has to do with int a=o;
#include <iostream>
using namespace std;
int main()
{
int c=1;
while (c != 0);
(
int a=0;
int b=0;
cout << "This program multiplys two numbers inputed by the user" << endl;
cout << endl << endl;
cout << "Please input a number." << endl;
cin >> a;
cout << "Please input a number." << endl;
cin >> b;
cout << "The numbers you enter equal";
cout << a * b << endl;
cout << "Thank you for using this program" << endl;
cout << "Would you like two continue using this program";
cout << " if so press any key except zero if you want to";
cout << " quit then press zero." << endl;
cin >> c;
return 0;
)
}
also here is my error
Compiler: Default compiler
Executing g++.exe...
g++.exe "D:\Documents and Settings\Justin\My Documents\555.cpp" -o "D:\Documents and Settings\Justin\My Documents\555.exe" -I"d:\Dev-Cpp\include\c++" -I"d:\Dev-Cpp\include\c++\mingw32" -I"d:\Dev-Cpp\include\c++\backward" -I"d:\Dev-Cpp\include" -L"d:\Dev-Cpp\lib"
D:/Documents and Settings/Justin/My Documents/555.cpp: In function `int
main()':
D:/Documents and Settings/Justin/My Documents/555.cpp:10: parse error before `=
' token
Execution terminated
Thank you for your help
Tech^:t
nothing
07-30-2004, 02:32 PM
Use curly brackets, not parenthesis in while loops, ifs, elses, for loops, etc.
while(true){
do stuff
do more stuff
}
if( 1 + 1 = 2){
do stuff
do more stuff
}
...and so on
Also, don't put a semicolon at the end of a while loop like you did:
while (c != 0);
If you do this, it is like the while loop is empty.
Hope that helps :t
Tech^salvager
07-30-2004, 07:11 PM
The program now complies correctly, but when I run it pressing any number for the loop will still exit it out. So I am guessing it has to do wth the loop. I am trying to figure it ot but I can't see where it exits it out and why it does that.
thanks for any help.
Tech^:t
Bone Collector
07-30-2004, 07:35 PM
you should remove this "return 0;" out of the loop, because that is the command to exit the program
cin >> c;
return 0;
}
replace with this
cin >> c;
}
return 0;
Tech^salvager
07-31-2004, 12:15 AM
Thank you Bone Collector
Now all to do is fix my code up more maybe cange int to float to let bigger numbers be used in the program.
Tech^salvager
09-29-2004, 12:19 PM
Hi guys well I am having a problem again.
when I put
cout << c = a * b;
it doesn't want to compile and gives one error now if I put
cout << a * b;
it works fine
but I want to assign it to c for use later
here is my code
#include <iostream>
//Version 0.0.36 of caluclator program
//Made by Justin R. Barrera
using namespace std;
int main()
{
int restart=1;
while (restart != 0) //Loop
{
float c=0, 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;
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;
default:
cout << "HAHA don't input that" << endl;
break;
}
cout << "To exit enter 0" << endl;
cin >> restart;
}
return 0;
}
Thanks
Tech^
ScaryBinary
09-29-2004, 01:04 PM
You can just do it in two statements, right?
switch (m)
{
case 1:
c = a * b ;
cout << "The product is " << c << endl;
break ;
case 2:
c = a / b ;
cout << "The quotient is " << c << endl;
break ;
...etc.
}
This assumes you're reading in "a" and "b" instead of "c" and "b" like in your sample.
If that's what you want, then you could even avoid some code by printing your output at the end, after the switch block.
case 1:
c = a * b ;
break ;
case 2:
c = a / b ;
break ;
...etc.
}
cout << "The answer is " << c << endl ;
Is this what you're looking for?
Tech^salvager
10-11-2004, 11:26 AM
Thanks
Sorry for not anwsering sooner.
Tech^salvager
10-12-2004, 11:20 AM
Ok back with questions Can I save all my output to a file?
How would I do that?
Thanks
Tech^
nothing
10-13-2004, 10:52 PM
#include <fstream>
int main()
{
int output = 1 + 1;
std::ofstream to_file("output.txt");
to_file << output << '\n';
to_file.close();
return 0;
}
That should do it ;)
Tech^salvager
10-14-2004, 10:00 AM
Is that
int input = 1 + 1;
that only writes 2 to the file
hmm well thats not it or else I am blind!
Well heres the code
Does it matter where that goes cause I didn't see if it matters since I tried different areas.
#include <iostream>
#include <fstream>
//Version 0.0.47 of caluclator program
//Made by Justin R. Barrera
using namespace std;
int main()
{
int restart=1;
while (restart != 0) //Loop
{
float c=0, 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;
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;
default:
cout << "HAHA don't input that" << endl;
break;
}
int output = restart + 1;
ofstream to_file("/output.txt");
to_file << output << endl;
to_file.close();
cout << "To exit enter 0" << endl;
cin >> restart;
}
return 0;
}
Thanks for the help
Tech^
I'm guessing that was just to show me that was how to start file I\O.
EDIT: for stupidiy cause I put the file output code in front before ( dang was I stupid ).
nothing
10-14-2004, 11:49 AM
Yes, I am just giving you a simple example so you can see how it is done. Now go try it on your own and come back if you have any problems :p
Tech^salvager
10-14-2004, 11:54 AM
Originally posted by nothing
Yes, I am just giving you a simple example so you can see how it is done. Now go try it on your own and come back if you have any problems :p he
I am so stupid
I bet I know how know.
nothing
10-14-2004, 12:00 PM
Try this:
#include <iostream>
#include <fstream>
//Version 0.0.47 of caluclator program
//Made by Justin R. Barrera
using namespace std;
int main()
{
int m;
int restart = 1;
float c, b;
ofstream to_file("output.txt", ios::app);
while (restart != 0) //Loop
{
c = 0;
b = 0;
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;
switch(m)
{
case 1:
cout << "The product is " << c * b << endl;
to_file << c * b << '\n';
break;
case 2:
cout << "The quotient is " << c / b << endl;
to_file << c / b << '\n';
break;
case 3:
cout << "The Difference is " << c - b << endl;
to_file << c - b << '\n';
break;
case 4:
cout << "The sum is " << c + b << endl;
to_file << c + b << '\n';
break;
default:
cout << "HAHA don't input that" << endl;
break;
}
cout << "To exit enter 0" << endl;
cin >> restart;
}
to_file.close();
return 0;
}
Tech^salvager
10-14-2004, 12:30 PM
WHY!
why
dang it now I understand it better!
well thanks
and did you write that up on your own or was a edit of my existing code?
Tech^
nothing
10-14-2004, 02:41 PM
I just changed your code a bit.
Tech^salvager
10-16-2004, 01:23 AM
nothing why are you using \n?
just a like to?
I can use endl right?
nothing
10-16-2004, 06:19 AM
Yes, they do the same thing. I use \n 'cause I think it is faster than endl.
Tech^salvager
10-18-2004, 01:59 PM
Well I added in validation for the floats.:t
:D
nothing
10-19-2004, 02:41 PM
Cool. Can we see it?
Tech^salvager
10-19-2004, 04:34 PM
Originally posted by nothing
Cool. Can we see it?
I am not sure if its true validation but I will put it up here.
#include <iostream>
#include <fstream>
//Version 0.0.122 of caluclator program
//Made by Justin R. Barrera
//WOWs
//Open Sourced under the Tech^salvager license
using namespace std;
int main()
{
//creating and opening file
ofstream to_file("/output.txt", ios::app);
// You have to use a variable with this while loop so we int yourbad variavle to 1
int yourbad=1;
//heres the condition it says if yourbad = 0 then stop loop but if it equals 1 then continue
while (yourbad != 0) //The while Loop
{
// seting my variables here
float a=0.0f, c=0.0f, b=0.0f;
int m=0;
cout << "Please type in a number then press enter " << endl;
cin >> c;
//So it doesn't except character input
while (!cin)
{
cout << "Please enter a valid number" << endl;
cin.clear ();
cin.ignore (1024,'\n');
cin >> c;
}
cout << "Please type in a number then press enter " << endl;
cin >> b;
//So it doesn't except character input
while (!cin)
{
cout << "Please enter a valid number" << endl;
cin.clear ();
cin.ignore (1024,'\n');
cin >> b;
}
cout << "Choose a mathmatical operation" << endl;
cout << "1=Multiplication, 2=Division, 3=Addtition, or 4=Subtraction" << endl;
cin >> m;
// I use case statments here to allow the user to pick their operation
// Yes you could use if statments or if else ones
switch(m)
{
case 1:
a = c * b;
cout << "The product is " << a << endl;
to_file << "The product of " << c << " multiplyed by " << b << " equals " << a << endl;
break;
case 2:
a = c / b;
cout << "The quotient is " << a << endl;
to_file << "The quotinet of " << c << " divided by " << b << " equals " << a << endl;
break;
case 3:
a = c - b;
cout << "The Difference is " << a << endl;
to_file << "The difference of " << c << " subtracted by " << b << " equals " << a << endl;
break;
case 4:
a = c + b;
cout << "The sum is " << a << endl;
to_file << "The sum of " << c << " added to " << b << " equals " << a << endl;
break;
default:
cout << "Loser" << endl;
break;
}
cout << "To exit enter the program press 0 then enter" << endl;
cout << "To start the program again press 1 then enter" << endl;
cin >> yourbad;
}
//closing the text file
to_file.close();
return 0;
}
nothing
10-19-2004, 07:32 PM
When working with numbers, I like to input them as strings. It is so much easier to validate them like that (in my opinion).
SysOpt.com
Copyright Internet.com Inc. All Rights Reserved.