Calculate two numbers in three ways

Hello guys!
I´m trying to write a program in where the user digit two numbers, and chooses either addition, multiplication or square-sum of these two numbers. The functions seems to work fine until the actual calculations. I´m a totally noob, so maybe I´m doing it all wrong... Hope anyone can explain what´s wrong.
Thank you!
//Fredrik


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  #include <iostream>
#include <cmath>
using namespace std;

// Function declaration

void skrivInfo();
void lasEttTal (int &tal1, int &tal2);
char lasOp();
void ordna (int &tal1, int &tal2);
int berakna (int tal1, int tal2, char op);
void skrivResultat (int summa);

// Main program

int main()
{
    int tal1, tal2, summa;
    char op;
    skrivInfo();
    lasEttTal (tal1, tal2);
    op = lasOp();
    ordna (tal1, tal2);
    summa = berakna (tal1, tal2, op);
    skrivResultat(summa);
    return 0;
}

// Func. def.
// Hello

void skrivInfo()
{
    cout<<"Hej det här programmet räknar addition, multiplikation eller kvadratsumman av två tal."<<endl;
}

//Func. lasEttTal that lets the user to write two numbers

void lasEttTal (int &tal1, int &tal2)
{
    cout<<"Skriv in två heltal: ";
    cin>>tal1>>tal2;
}

//Funk. lasOp that lets the user to choose which operation

char lasOp()
{
    char op;
    char t1='a';
    char t2='m';
    char t3='k';
    
    cout<<"Välj vilken beräkning som skall göras: "<<t1<<" addera, "<<t2<<" multiplicera, eller "<<t3<<" kvadratsumman av två tal: ";
    cin>>op;
    
    while (!((op==t1)||(op==t2)||(op==t3))) //if none of these (wrog choice)
    {
        cout<<"Fel inmatning"<<endl;
        cout<<"Välj vilken beräkning som skall göras "<<t1<<" addera, "<<t2<<" multiplicera, eller "<<t3<<" kvadratsumman av tal";
        cin>>op;
    }
    
    return op;
    };

//Func. to make them in order of magnitude
    
void ordna (int &tal1, int &tal2)
    
    {
        if(tal1>tal2)
        {
            int temp = tal1;
            tal1=tal2;
            tal2=temp;
        }
    }
    
// Func. to calculate according to choises in LasEttTal and in lasOp
    int berakna (int tal1, int tal2, char op)
    {
        int summa=0; //nollställa
        cout<<tal1<<endl;
        cout<<tal2<<endl;
        cout<<op<<endl;
        
        if(op=='a')
        {
            for(int tal1; tal1<=tal2; tal1++)
            {
                summa +=tal1; //addition
                cin>>summa;
                cout<<summa<<endl;
            }
        }
        
        if(op=='m')
        {
            for(int tal1; tal1<=tal2; tal1++)
                summa*=tal1; //multiplication
                cin>>summa;
                cout<<summa<<endl;
        }
        if(op=='k')
        {
            for(int tal1; tal1<=tal2; tal1++);
            summa=tal1*tal1 + tal2*tal2; // Square-sum
            cin>>summa;
            cout<<summa<<endl;
        }
        
        
          return summa;
        
}
// Func. write the result

void skrivResultat(int summa)
{
    
    cout<<"Resultatet av beräkningen är: "<<summa<<endl;
}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
if(op=='k')
        {
            for(int tal1; tal1<=tal2; tal1++); // <--- ?? does nothing
            summa=tal1*tal1 + tal2*tal2; // Square-sum
            cin>>summa;   // <----- this is wrong?**
            cout<<summa<<endl;
        }


** this is just an example of a possible error.

Here you are calculating summa, then getting a new value via cin, then cout'ing the new value.

This doesn't make sense because why would you calculate the first summa and not use it?
Last edited on
closed account (48T7M4Gy)
PS I just noticed you do the same with each operation.

Also the for loop is doing nothing and besides it has a syntax problem. I suggest you comment ot that line.

should be:

1
2
3
4
5
if (op == ...)
{
   summa = ... ;
   cout << summa etc << endl;
}

Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Func. to calculate according to choises in LasEttTal and in lasOp

int berakna (int tal1, int tal2, char op)
    {
        int summa=0; //nollställa
        cout<<tal1<<endl;
        cout<<tal2<<endl;
        cout<<op<<endl;
        
        if(op=='a')
           summa = tal1 + tal2; //addition

        if(op=='m')
           summa = tal1 * tal2; //multiplication

        if(op=='k')
           summa=tal1*tal1 + tal2*tal2; // Square-sum

          cout << summa;
          return summa;
}


or, you can have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int berakna (int tal1, int tal2, char op)
    {
        cout<<tal1<<endl;
        cout<<tal2<<endl;
        cout<<op<<endl;
        
        if(op=='a')
           return tal1 + tal2; //addition

        if(op=='m')
           return tal1 * tal2; //multiplication

        if(op=='k')
           return tal1*tal1 + tal2*tal2; // Square-sum

          cout << "Error";
          return 0;
}
closed account (48T7M4Gy)
also in main()

cout << berakna (3, 4, 'k') << endl;

or

1
2
int xyz = berakna (3, 4, 'k');
cout << xyz << endl;


Should display 25 in each case. E&OE
Thanks Kemort! Logically, "cin" is wrong, and I erased it on all three (I don´t know why I but it there. The reason for "for"-loop is that I want lowest number and the highest number, but also the numbers in between (integer): 5 and 7: Addition > 5+6+7=18. I´m doing it wrong in some way...
Yours
Fredrik
closed account (48T7M4Gy)
OK
a for loop looks like this, I suggest you try it yourself.

Say you want to print out all the numbers from 1 to 6, and get their total

1
2
3
4
5
6
7
8
int summa = 0;
for ( int i = 1; i <= 6; i++ )
{
    summa += i;
    cout << i << ' ' << summa << endl;
}

cout << "Summa:  


http://www.cplusplus.com/doc/tutorial/control/
Last edited on
I´ll try that. Thanks!!
Topic archived. No new replies allowed.