+ 1
Need help! Please (C++)
Im currently newbie in C++. I wanted to make a calculator that gives you selection of list wich one u wanna do. for example. 1 for + 2 for - 3 for * and 4 for /. i couldnt make the selection so could you guys help me? Heres code i made. (i know it may be a mess lol)
11 Réponses
+ 2
You do not ask for input before you go into the switch,
also you use too many switches, 1 is enough.
Remember that if you declare variables inside a switch you should use {} if you use more than 1 case or else you will get errors and break after every case.
Anyway, you should first ask for input, 1 to 4
and in 1 switch you then handle the operations.
You repeat the input alot so you can make a function to decrease the code duplication.
Lastly remember that / is a special case where you should get a floating point number back, not an integer.
int / int = int, so you'll have to cast to a float
float / int = float
Here's an example I quickly made:
#include <iostream>
void getInput(int& a, int& b)
{
std::cout << "Enter first number" << std::endl;
std::cin >> a;
std::cout << "Enter second number" << std::endl;
std::cin >> b;
}
int main()
{
std::cout << "Enter 1 for +\n" <<
"Enter 2 for -\n" <<
"Enter 3 for *\n" <<
"Enter 4 for /\n\n";
int op;
std::cin >> op;
switch(op)
{
case 1: // +
{
int a, b;
getInput(a,b);
std::cout << a << " + " << b << " = " << a + b << std::endl;
break;
}
case 2: // -
{
int a, b;
getInput(a,b);
std::cout << a << " - " << b << " = " << a - b << std::endl;
break;
}
case 3: // *
{
int a, b;
getInput(a,b);
std::cout << a << " * " << b << " = " << a * b << std::endl;
break;
}
case 4: // /
{
int a, b;
getInput(a,b);
std::cout << a << " / " << b << " = " << static_cast<float>(a) / b << std::endl;
break;
}
default:
std::cout << "Invalid option" << std::endl;
}
return 0;
}
+ 1
int main()
{
int a, b;
int n;
cout << "enter first number:\n";
cin >> a;
cout << "enter second number:\n";
cin >> b;
cout << "which operation do you want to do?" << endl;
cout << "1) + , 2) - , 3) * , 4) /" << endl;
cout << "enter number of the operation:\n";
cin >> n;
switch (n)
{
case 1:
cout << "a + b = " << a + b;
break;
case 2:
cout << "a - b = " << a - b;
break;
case 3:
cout << "a * b = " << a * b;
break;
case 4:
cout << "a / b = " << a / b;
break;
default:
cout << "invalid operation";
break;
}
cout << endl;
return 0;
}
this is my code
+ 1
It's a function, it doesn't belong in main.
If you use the same code in different places you should go for a function instead to increase readability and reduce the chance of errors.
I dislike 'using namespace std;'
I often implement functions that use the same name as a function in the std library.
If I used 'using namespace std;' I would either get an error(if I'm lucky) or get undefined behavior.
'using namespace std;' is considered bad practice and you should try to avoid it.
Don't use it in global scope.
Never ever use it within header files.
It's sometimes ok when used in local scope, although there are still better alternatives.
Lastly I think std::cout, etc... looks better than cout, etc....
I'm so used to it that code without it just looks weird.
+ 1
Try it without, you'll see that a and b remain unchanged after the function.
Without & the value passed to the function would be copied and the copied value would be changed and then destroyed when the function ends.
With & the address of the value is passed and the value at that address will then be changed. This change is then visible outside the function.
0
here is the code
#include <iostream>
using namespace std;
int main()
{
int plussa = 1;
switch(plussa){
case 1:
cout << "Enter your first number " << endl;
int a;
cin >> a;
cout << "Enter your second number " << endl;
int b;
cin >> b;
int summa = a + b;
cout << "Answer is" << endl;
cout << summa << endl;
}
int miinus = 2;
switch(miinus){
case 2:
cout << "Enter your first number " << endl;
int a;
cin >> a;
cout << "Enter your second number " << endl;
int b;
cin >> b;
int miinus = a - b;
cout << "Answer is" << endl;
cout << miinus << endl;
}
int kerto = 3;
switch(kerto){
case 3:
cout << "Enter your first number "<< endl;
int a;
cin >> a;
cout << "Enter your second number " << endl;
int b;
cin >> b;
int kerto = a * b;
cout << "Answer is" << endl;
cout << kerto << endl;
}
int jako = 4;
switch(jako){
case 1:
cout <<"Enter your first number " << endl;
int a;
cin >> a;
cout << "Enter your second number " << endl;
int b;
cin >> b;
int jako = a / b;
cout << "Answer is" << endl;
cout << jako << endl;
}
}
0
Thank you sir! I can read codes, but somehow i can't make my owns :/
0
Hehe, you're not the only one.
I also had that problem when I started out, but keep trying and soon you'll be writing code without needing to look stuff up. :)
0
Dennis, I got a question tho. Why you didn't put that part in main.
void getInput(int& a, int& b)
{
std::cout << "Enter first number" << std::endl;
std::cin >> a;
std::cout << "Enter second number" << std::endl;
std::cin >> b;
And why you didn't use: using namespace std; ?
0
1 More question :P
void getInput(int& a, int& b)
why did you put & after int? what does it mean
0
I love you man! How long you've been into C++? And in how long time you think it's possible to master it? :D
0
I started programming with C++ about 1.5 years ago.
I learned the basics from a book, it is in dutch though, so unless you can understand that there isn't much sense in recommending it :P
After I finished that I went to
https://projecteuler.net/archives
to learn some more about math/algorithms and optimization techniques.
I also randomly picked some challenges from here:
http://rosettacode.org/wiki/Category:Programming_Tasks
There are some really interesting topics there.
About a year ago I stumbled upon
https://www.codingame.com/
Which provides an easy way to learn AI programming, it's where I learned stuff like path finding algorithms.
You can also challenge up to 7 people at the same time in the Clash of Code section where you are given a random task and you have to complete it within 15 minutes.
You can learn alot from other peoples submissions.
To master anything really, people say you need to spend 10.000 hours in it, sure people learn at different rates but that's about the target you should aim for if you are really serious.
Currently I'm making simple copies of old games, like snake using the SFML library.
Still a long way to go till the 10k mark ^^, gl to you on your journey. :)