| View previous topic :: View next topic |
| Author |
Message |
spectrum Expert Cheater
Reputation: 0
Joined: 27 Mar 2007 Posts: 143
|
Posted: Tue Jan 22, 2008 5:48 pm Post subject: my first program in c++ |
|
|
| Code: | // my first program in C++
#include <iostream>
using namespace std;
int a, b;
char c;
int main ()
{
while
cout << "this is a calculator, press 1 for sum, 2 for rest 3 for division and 4 for multiplication"<< endl;
cin >> a >> c >> b;
if (c = 1)
cout << (a + b);
if (c = 2)
cout << (a - b);
if (c = 3)
cout << (a / b);
if (c = 4)
cout << (a * b) << endl;
system("pause")
return 0;
}
|
well im just showing what im learning , i started reading tuts yesterday.
well this calculator has some flaws, any tips on how to optimize it would be very appreciated!
|
|
| Back to top |
|
 |
Symbol I'm a spammer
Reputation: 0
Joined: 18 Apr 2007 Posts: 5094 Location: Israel.
|
Posted: Tue Jan 22, 2008 5:51 pm Post subject: |
|
|
You should use switch instead of "if" and don't use System("PAUSE")...
The rest is ok.
|
|
| Back to top |
|
 |
Heartless I post too much
Reputation: 0
Joined: 03 Dec 2006 Posts: 2436
|
Posted: Tue Jan 22, 2008 5:53 pm Post subject: |
|
|
Weird, my first program was the Hello World program, but this is very good.
_________________
What dosen't kill you, usually does the second time. |
|
| Back to top |
|
 |
spectrum Expert Cheater
Reputation: 0
Joined: 27 Mar 2007 Posts: 143
|
Posted: Tue Jan 22, 2008 6:15 pm Post subject: |
|
|
| Symbol wrote: | You should use switch instead of "if" and don't use System("PAUSE")...
The rest is ok. |
no idea what that is...i'll keep reading
|
|
| Back to top |
|
 |
onfia Cheater
Reputation: 0
Joined: 18 Jan 2008 Posts: 45
|
Posted: Tue Jan 22, 2008 6:17 pm Post subject: |
|
|
| He means one he made on his own and yea c++ is very fun, keep at it bro.
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Tue Jan 22, 2008 7:10 pm Post subject: |
|
|
You're not using c right. You are actually comparing to a number instead of a char. If you are going to use if statements (switch is the best way), use if...else if. The way you are getting the input is off. You should get c first then get a and b. When using a while loop you have to do it like this:
| Code: |
while (<variable to express true>)
{
// Insert code here
}
|
The way you have it, it will not compile. I've attached a .cpp if you want a reference.
|
|
| Back to top |
|
 |
spectrum Expert Cheater
Reputation: 0
Joined: 27 Mar 2007 Posts: 143
|
Posted: Tue Jan 22, 2008 9:51 pm Post subject: |
|
|
Ok so now im trying to make it using switch, it seems it doesn't work. the result isn't shown here is the code: | Code: | // my first program in C++
#include <iostream>
using namespace std;
int a, b;
char c;
string answer;
int main ()
{
cout << "this is a calculator" << endl;
{
cout << "choose what operation you want to make \
(1 for sum, 2 for rest, 3 for multiplication and 4 for division" << endl;
cin >> c;
cout << "choose the first number" << endl;
cin >> a;
cout <<"now choose the second"<< endl;
cin >> b;
cout << "the answer is: ";
switch (c) {
case 1:
cout << a + b << endl;
break;
case 2:
cout << a - b << endl;
break;
case 3:
cout << a * b << endl;
break;
case 4:
cout << a / b << endl;
break;
}
}
ci
return 0;
}
|
mind helping?
sorry i found the error , c was still a char var
say hello to my calculator | Code: | // my first program in C++
#include <iostream>
using namespace std;
int a, b, c;
string answer;
int main ()
{
cout << "this is a calculator" << endl;
{
cout << "choose what operation you want to make \
(1 for sum, 2 for rest, 3 for multiplication and 4 for division" << endl;
cin >> c;
cout << "choose the first number" << endl;
cin >> a;
cout <<"now choose the second"<< endl;
cin >> b;
cout << "the answer is: ";
switch (c) {
case 1:
cout << a + b << endl;
break;
case 2:
cout << a - b << endl;
break;
case 3:
cout << a * b << endl;
break;
case 4:
cout << a / b << endl;
break;
}
}
system("PAUSE");
return 0;
} |
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Wed Jan 23, 2008 5:11 am Post subject: |
|
|
| spectrum wrote: |
say hello to my calculator | Code: | // my first program in C++
// my first program in C++
#include <iostream>
using namespace std;
int a, b, c;
string answer;
int main ()
{
cout << "this is a calculator" << endl;
{
cout << "choose what operation you want to make \
(1 for sum, 2 for rest, 3 for multiplication and 4 for division" << endl;
cin >> c;
cout << "choose the first number" << endl;
cin >> a;
cout <<"now choose the second"<< endl;
cin >> b;
cout << "the answer is: ";
switch (c) {
case 1:
cout << a + b << endl;
break;
case 2:
cout << a - b << endl;
break;
case 3:
cout << a * b << endl;
break;
case 4:
cout << a / b << endl;
break;
}
}
system("PAUSE");
return 0;
} |
|
I will do some coding and put it on this post or in a new one if some one posts after me.
It is still better than my first program.
Edit: Here i did some coding on it not very good just what i threw together fast.
| Code: | // Lets see im a little rusty
// Could make this more advanced
//
#include <iostream> // for basic functions
// Next comes the prototypes
int input(int min, int max); // It will be used to protect the switch from invalid input
double getinput(); // Function gets the input
// The next function could help control invalid input that is too big
// I'm commenting it and also going to comment the function with /* */
// Just remove the /* before the function and the */ after to use it
// Also there will be a line calling this in getinput()
//double getinput(double num ,double min, double max);
double square(); // Im using functions to do the math
double add(); // Very simple does addition
double sub(); // This does subtraction
double dev(); // Devides
double x(); // Multiplies
int main ()
{
double a = 0; // Makes the variable and sets it to 0
//
int c = 0; // Int variable to control the switch that im going to make
bool looping = true; // Boolean value can be true(1) or false(0)
//I will use this for a simple way to break the infinite loop
// Bool is helpful for making error messages in a program.
// Now im saying what the program is
std::cout << " Welcome to the calculater program\n";
// Next i add a infinite loop that will break when looping = false
for(;;)
{
// Prompts for input on what operation to do. Also added squaring
std::cout << "Choose what you whant to do.\n" << "0 to quit, "
<< "1 to add, " << "2 to subtract, " << "3 to do multiply, "
<< "4 to do division, " << " And 5 to square: ";
c = input(0,5);
// If you add more options replace the 5 with the max number of
// options
// also add more cases to the switch
switch(c)
{
case 0:
looping = false; // Will stop the loop
std::cout << "~crys~ Will i ever see you again?\n";
break;
case 1:
add();
break;
case 2:
sub();
break;
case 3:
x();
break;
case 4:
dev();
break;
case 5:
square();
break;
}
}
}
int input(int min,int max)
{
int input = 0;
std::cin >> input;
while(std::cin.fail()||input>max||input<min) // checks if input is bad
{
std::cout << "Bad Input\n";
std::cout << "Try again: ";
std::cin.clear(); // clears the failbit
std::cin.sync(); // sync stream
std::cin >> input; // gets input again
}
return input;
}
double square()
{
double num1;
std::cout << "Enter a number: ";
num1 = getinput();
std::cout << "Answer is " << num1*num1 << std::endl << std::endl;
}
double add()
{
double num1, num2;
std::cout << "Please enter a number: ";
num1 = getinput();
std::cout << "Enter the number to add: ";
num2 = getinput();
std::cout << "The answer is " << num1+num2 << std::endl << std::endl;
}
double sub()
{
double num1, num2;
std::cout << "Please enter a number: ";
num1 = getinput();
std::cout << "Enter the number to subtract: ";
num2 = getinput();
std::cout << "The answer is " << num1-num2 << std::endl << std::endl;
}
double dev()
{
double num1, num2;
std::cout << "Please enter a number: ";
num1 = getinput();
std::cout << "Enter the number to devide by: ";
std::cin >> num2;
std::cout << "The answer is "<< num1/num2 << std::endl << std::endl;
}
double x()
{
double num1, num2;
std::cout << "Please enter a number: ";
num1 = getinput();
std::cout << "Enter the number to multiply by: ";
std::cin >> num2;
std::cout << "The answer is "<< num1*num2 << std::endl << std::endl;
}
double getinput()
{
double input = 0;
std::cin >> input;
while(std::cin.fail()) // checks if input is bad
{
std::cout << "Bad Input";
std::cin.clear(); // clears the failbit
std::cin.sync(); // sync stream
std::cin >> input; // gets input again
}
return input;
}
|
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
spectrum Expert Cheater
Reputation: 0
Joined: 27 Mar 2007 Posts: 143
|
Posted: Wed Jan 23, 2008 9:10 am Post subject: |
|
|
whoa, you threw that "fast"? its excelent i tested it! i learned alot from the comments
whats the difference between:
| Code: |
std::cout << "Bad Input\n";
|
and
| Code: |
cout << "bad input\n";
|
?
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Wed Jan 23, 2008 1:15 pm Post subject: |
|
|
| spectrum wrote: | whoa, you threw that "fast"? its excelent i tested it! i learned alot from the comments
whats the difference between:
| Code: |
std::cout << "Bad Input\n";
|
and
| Code: |
cout << "bad input\n";
|
? |
You will notice a few errors. Like for one on this section of the code.
| Code: | for(;;)
{
// Prompts for input on what operation to do. Also added squaring
std::cout << "Choose what you whant to do.\n" << "0 to quit, "
<< "1 to add, " << "2 to subtract, " << "3 to do multiply, "
<< "4 to do division, " << " And 5 to square: ";
c = input(0,5);
// If you add more options replace the 5 with the max number of
// options
// also add more cases to the switch
switch(c)
{
case 0:
looping = false; // Will stop the loop
std::cout << "~crys~ Will i ever see you again?\n";
break;
case 1:
add();
break;
case 2:
sub();
break;
case 3:
x();
break;
case 4:
dev();
break;
case 5:
square();
break;
}
} |
You notice i forgot to add a | Code: | | if(looping == false) { break;} |
And if you think about it it would be better if i did this instead.
| Code: | while(looping == true)
{
// Prompts for input on what operation to do. Also added squaring
std::cout << "Choose what you whant to do.\n" << "0 to quit, "
<< "1 to add, " << "2 to subtract, " << "3 to do multiply, "
<< "4 to do division, " << " And 5 to square: ";
c = input(0,5);
// If you add more options replace the 5 with the max number of
// options
// also add more cases to the switch
switch(c)
{
case 0:
looping = false; // Will stop the loop
std::cout << "~crys~ Will i ever see you again?\n";
break;
case 1:
add();
break;
case 2:
sub();
break;
case 3:
x();
break;
case 4:
dev();
break;
case 5:
square();
break;
}
} |
And another thing in my code you should'nt of missed is this
| Code: | // The next function could help control invalid input that is too big
// I'm commenting it and also going to comment the function with /* */
// Just remove the /* before the function and the */ after to use it
// Also there will be a line calling this in getinput()
//double getinput(double num ,double min, double max); |
Originally i planned something completely diffrent and forgot to remove that comment.
I will show how that would of worked in the code on a edit to this post or a new one.
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Wed Jan 23, 2008 1:25 pm Post subject: |
|
|
std:: is a prefix to access the std namespace (where cout, cin, endl and ect, are located
std::cout is just to access the cout function.
most people just do this because there lazy and dont want to add std:: before them all (i find this pointless and bad form)
| Code: | | using namespace std; |
or u can also do this, which is a good alternative (instead of adding the entire namespace, just define ur using cout, cin, and endl
| Code: | using std::cout;
using std::cin;
using std::endl; |
_________________
|
|
| Back to top |
|
 |
Losplagos Expert Cheater
Reputation: 0
Joined: 21 Mar 2006 Posts: 172 Location: Knee deep in a c++ book
|
Posted: Wed Jan 23, 2008 2:09 pm Post subject: |
|
|
My sleep deprived mind is'nt thinking right it would be a waste to add another function. Also this is as basic as i can make this code right now. I also fixed a mistake i repeated.
Instead of
| Code: | // Lets see im a little rusty
// Could make this more advanced
//
#include <iostream> // for basic functions
// Next comes the prototypes
int input(int min, int max); // It will be used to protect the switch from invalid input
double getinput(); // Function gets the input
double square(); // Im using functions to do the math
double add(); // Very simple does addition
double sub(); // This does subtraction
double dev(); // Devides
double x(); // Multiplies
int main ()
{
double a = 0; // Makes the variable and sets it to 0
//
int c = 0; // Int variable to control the switch that im going to make
bool looping = true; // Boolean value can be true(1) or false(0)
//I will use this for a simple way to break the infinite loop
// Bool is helpful for making error messages in a program.
// Now im saying what the program is
std::cout << " Welcome to the calculater program\n";
// Next i add a infinite loop that will break when looping = false
while(looping == true)
{
// Prompts for input on what operation to do. Also added squaring
std::cout << "Choose what you whant to do.\n" << "0 to quit, "
<< "1 to add, " << "2 to subtract, " << "3 to do multiply, "
<< "4 to do division, " << " And 5 to square: ";
c = input(0,5);
// If you add more options replace the 5 with the max number of
// options
// also add more cases to the switch
switch(c)
{
case 0:
looping = false; // Will stop the loop
std::cout << "~crys~ Will i ever see you again?\n";
break;
case 1:
add();
break;
case 2:
sub();
break;
case 3:
x();
break;
case 4:
dev();
break;
case 5:
square();
break;
}
}
return EXIT_SUCCESS;
}
int input(int min,int max)
{
int input = 0;
std::cin >> input;
while(std::cin.fail()||input>max||input<min) // checks if input is bad
{
std::cout << "Bad Input\n";
std::cout << "Try again: ";
std::cin.clear(); // clears the failbit
std::cin.sync(); // sync stream
std::cin >> input; // gets input again
}
return input;
}
double square()
{
double num1;
std::cout << "Enter a number: ";
num1 = getinput();
std::cout << "Answer is " << num1*num1 << std::endl << std::endl;
}
double add()
{
double num1, num2;
std::cout << "Please enter a number: ";
num1 = getinput();
std::cout << "Enter the number to add: ";
num2 = getinput();
std::cout << "The answer is " << num1+num2 << std::endl << std::endl;
}
double sub()
{
double num1, num2;
std::cout << "Please enter a number: ";
num1 = getinput();
std::cout << "Enter the number to subtract: ";
num2 = getinput();
std::cout << "The answer is " << num1-num2 << std::endl << std::endl;
}
double dev()
{
double num1, num2;
std::cout << "Please enter a number: ";
num1 = getinput();
std::cout << "Enter the number to devide by: ";
num2= getinput();
std::cout << "The answer is "<< num1/num2 << std::endl << std::endl;
}
double x()
{
double num1, num2;
std::cout << "Please enter a number: ";
num1 = getinput();
std::cout << "Enter the number to multiply by: ";
num2= getinput();
std::cout << "The answer is "<< num1*num2 << std::endl << std::endl;
}
double getinput()
{
double input = 0;
std::cin >> input;
while(std::cin.fail()) // checks if input is bad
{
std::cout << "Bad Input";
std::cin.clear(); // clears the failbit
std::cin.sync(); // sync stream
std::cin >> input; // gets input again
}
return input;
}
|
Only thing i cant think to fix is if you enter something like 1x or a number then a letter. It also screws up if you put a number like 1.2 or 3.1337.
Hope you can pickup something worthwhile from this.
_________________
Earthbound = 31337 |
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Wed Jan 23, 2008 2:37 pm Post subject: |
|
|
| Make sure your functions return a value. If not, make them void not double. You should get a warning or an error depending on your compiler settings. It is also the correct way of coding if the function is not void.
|
|
| Back to top |
|
 |
spectrum Expert Cheater
Reputation: 0
Joined: 27 Mar 2007 Posts: 143
|
Posted: Wed Jan 23, 2008 6:53 pm Post subject: |
|
|
thanks alot people!
now i was thinking if it was very hard to change the gui, cause i've seen a tut and its like a completely different language(http://www.winprog.org/tutorial/simple_window.html)
that's like a tut for win, but im saying: is that the only way to do it? cause it seems pretty hard
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Wed Jan 23, 2008 7:02 pm Post subject: |
|
|
winprog is a tutorial for Win32 API C++ (Hard Coding), if u want u can use C++.NET (Drag and Drop) but i dont recomend it, if your going to do .NET windows then id go to C# for that.
_________________
|
|
| Back to top |
|
 |
|