-5
#include <stdio.h>
#include <string>

int main(){
    float a,b,z,u,r,k;
    char f,q,m;
    system("color B");
repeat:
    printf("Unesite broj a:  ");
    scanf("%f",&a);
repeatm:
    printf("Unesite broj b:  ");
    scanf("%f",&b);
    printf("Unesite funkciju (+,-,*,/):  ");
finput:
    scanf("%s",&f);
    if(f=='+'){
        goto zbroj;  
       }else{
        if(f=='-'){
            goto razlika;
        }else{
            if(f=='*'){
                goto umnozak;
            }else{
                if(f=='/'){
                    goto kolicnik;
                }else{
                    goto _newf;
                }
            }
        }
    }
//input of numbers and a check if the value of 'f' is one of the characters +,-,* or /
zbroj:
    z=a+b;
    printf("-------------------------------------\n");
    printf("Zbroj ta dva broja je: ");
    printf("%f \n",z);
    goto _new;
razlika:
    r=a-b;
    printf("-------------------------------------\n");
    printf("Razlika ta dva broja je: ");
    printf("%f \n",r);
    goto _new;
umnozak:
    u=a*b;
    printf("-------------------------------------\n");
    printf("Umnozak ta dva broja je: ");
    printf("%f \n",u);
    goto _new;
kolicnik:
   if(b==0){
        system("cls");
        printf("Ne mozete dijeliti s nulom \n");
        goto repeatB;
    }else{
 k=a/b;
    printf("-------------------------------------\n");
    printf("Kolicnik ta dva broja je: ");
    printf("%f \n",k);
    goto _new;
    }
//prints results
_newf:
    printf("Nevazeca funkcija, upisite ponovo:  ");
    goto finput;
//loop if 'f' isnt one of +,-,*,/
_new:
    printf("-------------------------------------\n");
    printf("Novi racun?(y/n)  ");
    scanf("%s",&q);
    system("cls");
    goto repeat1;
//asks if i want to exit or go back to start
repeat1:
    if(q=='y'){
        system("cls");
        goto memory;
    }else{
        if(q=='n'){
            goto exit;
        }else{
            printf("Novi racun? \n");
            printf("Molimo upisite y ili n:  ");
            scanf("%s", &q);
            goto repeat1;
        }
    }
//loops back to start
memory:
    printf("Zelite li iskoristits peredhodni rezultat kao broj a?(y/n):  ");
    scanf("%s", &m);
//asks if i want the previous result as the number 'a'
repeat2:
    if(m=='y'){
        system("cls");
        goto printfa;
    }else{
        if(m=='n'){
            system("cls");
            goto repeat;
        }else{
            printf("Zelite li iskoristits peredhodni rezultat kao broj a? \n");
            printf("Molimo upisite y ili n:  ");
            scanf("%s", &m);
            goto repeat2;
        }
    }
//checks if variable 'm' is either y or n
printfa:
    if(f=='+'){
        a=z;     
    }else{
        if(f=='-'){
            a=r;
        }else{
            if(f=='*'){
                a=u;
            }else{
                if(f=='/'){
                    a=k;       
                }
            }
        }
    }
//this should(?) overwrite the variable 'a'
//why isnt 'a' overwritten with the value of 'z', 'r', 'u' or 'k' here?
    printf("Broj a je:  %f \n", a); 
//here the value of 'a' stays the same as it was before
    goto repeatm;
repeatB:
    printf("Unesite broj b:  ");
    scanf("%f",&b);
    goto kolicnik;
exit:
    system("cls");
    system("pause");
    return(0);
}

is there any way to overwrite the variable 'a' (without manual input) with one of the values of 'z', 'r', 'u' or 'k'. the result of this code is just a + (or -,*,/) new b (which i manualy input again, but the variable a stays the same as the first input). pleas dont mind the foreign language it just says "input a", "input b", "the result is..", also sorry if the code is messy..

Tim Pavic
  • 13
  • 2
  • 7
    A) this is not C++ code. It might compile in a C++ compiler but this is not what C++ code should look like. B) please forget `goto` exists. Use loops and functions instead. You can revisit `goto` once you start getting into really advanced topics. – NathanOliver Feb 27 '19 at 21:46
  • Also please consider elseif instead of increasingly nesting your clauses. I think you might have a hard time finding people who are going to encourage you to write code with gotos in it. – Joseph Larson Feb 27 '19 at 21:48
  • 1
    [Spaghetti code](https://en.wikipedia.org/wiki/Spaghetti_code). Not only will you have trouble finding persons encouraging this type of coding, you will have a harder time finding persons willing to help if your program is written this way. No one is going to try and untangle spaghetti -- instead, redesign your code to use the proper structured programming constructs (`for`, `while`, `do-while`, separate code into functions, etc.). It seems you wrote your program in an ad-hoc style (I need to do "x", so create a block of code called "x" and go there). Don't program this way. – PaulMcKenzie Feb 27 '19 at 21:53
  • BTW `scanf("%s", &q);` should be `scanf(" %c", &q);` – Iłya Bursov Feb 27 '19 at 21:59
  • 1
    If someone gave you that code, they're an evil who wishes you to fail. If you wrote that code, I strongly recommend [upgrading your C++ reference library](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Feb 27 '19 at 21:59
  • yeah, so. im just learning the basics atm and ive read about how i should avoid goto and stuff, but thats the simplest way ive managed to make this thing work. all i know rn is that everything works except that printfa block. anyways thanks for the advice – Tim Pavic Feb 27 '19 at 23:24
  • 1
    @TimPavic *but thats the simplest way ive managed to make this thing work.* -- That is equivalent to saying "I cleaned up my room by sweeping everything under the carpet". The room may look clean, but it isn't. You were lucky someone took your code, took time to untangle it, and thus the flow of the code is easier to follow. Again, do not write programs in the fashion I mentioned, i.e. if you need to do a task, create a `goto` to that task. Then what happens when you need to exit that task? Create another `goto` to go somewhere else. Then everything becomes a tangled mess. – PaulMcKenzie Feb 28 '19 at 06:34

1 Answers1

1

I can only answer the question indirectly, because I didn't start looking into the actual problem until after I had cleaned the code up a little bit - but now I don't get the error. So, the answer is: clean up the code and it'll probably work as expected.

I've kept saving the result in different variables although nothing in the program needs to have more than one result variable as it is right now. The main flow of the program should be like your original but it's just a tad easier to read now:

#include <iostream>
#include <string>

// helper functions
bool yes_or_no(const std::string& question) {
    char answer;
    do {
        std::cout << question << " (y/n)  ";
        std::cin >> answer;
    } while(answer != 'y' && answer != 'n');
    return answer == 'y';
}

void line() { std::cout << "-------------------------------------\n"; }

void display_result(const std::string& heading, float value) {
    std::cout << heading << " ta dva broja je: " << value << "\n";
}

int main() {
    float op_a, op_b, add_res = 0, mul_res = 0, sub_res = 0, div_res = 0;
    char oper;
    bool save_op_a = false;
    system("color B");

    while(true) {
        system("cls");
        if(save_op_a == false) {
            std::cout << "Unesite broj a:  ";
            std::cin >> op_a;
        } else
            save_op_a = false;

        std::cout << "Unesite broj b:  ";
        std::cin >> op_b;

        while(true) {
            std::cout << "Unesite funkciju (+,-,*,/):  ";
            std::cin >> oper;
            line();
            if(oper == '+') {
                add_res = op_a + op_b;
                display_result("Zbroj", add_res);
                break;
            } else if(oper == '-') {
                sub_res = op_a - op_b;
                display_result("Razlika", sub_res);
                break;
            } else if(oper == '*') {
                mul_res = op_a * op_b;
                display_result("Umnozak", mul_res);
                break;
            } else if(oper == '/') {
                if(op_b == 0.) {
                    std::cout << "Ne mozete dijeliti s nulom\n";
                    continue;
                }
                div_res = op_a / op_b;
                display_result("Kolicnik", div_res);
                break;
            }
        }

        line();

        if(yes_or_no("Novi racun?")) {
            system("cls");
            // asks if i want the previous result as the number 'op_a'
            if(yes_or_no("Zelite li iskoristits peredhodni rezultat kao broj a?")) {
                // this should(?) overwrite the variable 'op_a'
                // why isnt 'op_a' overwritten with the value here?
                system("cls");
                if(oper == '+') {
                    op_a = add_res;
                } else if(oper == '-') {
                    op_a = sub_res;
                } else if(oper == '*') {
                    op_a = mul_res;
                } else if(oper == '/') {
                    op_a = div_res;
                }
                // here the value of 'op_a' stays the same as it was before
                std::cout << "Broj a je:  " << op_a << "\n";
                save_op_a = true;
            }
        } else
            break; // exit
    }

    system("cls");
    system("pause");
    return (0);
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • holy.. i dint expect someone to translate my whole code into iostream. thank you, like a lot. ill spend the rest of the night trying to learn how this works (ive only had expirience with stdio.h until now). thank you. – Tim Pavic Feb 28 '19 at 00:05
  • @TimPavic You should be praising the fact that the OP took your code and untangled it from that `goto` mess. That is worth far more than simply using or not using `iostream`. – PaulMcKenzie Feb 28 '19 at 06:36
  • Tim, you''re welcome, and @PaulMcKenzie is correct, making use of iostreams was just a little osmetic change that took no effort. The goto jungle was more time consuming to get through and it was probably in there somewhere that the bug disappeared. Probably ... – Ted Lyngmo Feb 28 '19 at 06:48