|
-
October 8th, 2015, 01:40 PM
#1
Calculating 2 numbers in 3 ways: code doesn´t work
Hi,
I´m trying to learn C++, and I´m currently making a small program, but my code doesn´t work. Maybe someone here knows what I´m doing wrong!
I want the user to digit two numbers, and choose a=addition, m=multiplication and k=square-sum. And this does work with the two number, but I want to include al the numbers between the two numbers: for example, 3 and 5, with "a" = 3+4+5=12.
Hope someone could help!!
Here´s my idiotic code:
// Func. to calculate according to choises in LasEttTal and in lasOp
int berakna (int tal1, int tal2, char op)
{
int summa=0; //make it zero
cout<<tal1<<endl;
cout<<tal2<<endl;
cout<<op<<endl;
if(op=='a')
{
for(int i=tal1; i<=tal2; i++) //tal1 and tal 2 is the two choosen numbers
{
summa=tal1+tal2; //addition
cout<<i<<summa<<endl;
}
}
if(op=='m')
{
for(int i=tal1; i<=tal2; i++)
{
summa=tal1*tal2; //multiplication
cout<<i<<summa<<endl;
}
}
if(op=='k')
{
for(int i=tal1; i<=tal2; i++)
{
summa=(tal1*tal1) + (tal2*tal2); // Square-sum
cout<<i<<summa<<endl;
}
}
return summa;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|