if/if-else/if-else-if/goto etc

Please help with my HW. So as it goes below:
Solve the equation a+b-c which will be repetead X10 times, a,b,c will get random values from 1-10 interval.
Follow the conditions :
If a+b-c > 10, add an iteration.
If a+b-c = 10, subtract an iteration.
Count how many times value "a" was even.
Count how many times value "c" was odd.
Count how many times a+b-c was odd and even.

That's my 6-th lesson so I am only at the beggining, don't judge me hard, check the code below:

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
#include <iostream>
  #include <cstdlib> 
  #include <ctime>

  using namespace std;

 int main()
  {
      srand(time(0));

int a,b,c, i=0, x=10, par=0, impar=0, rez_pare=0, rez_impare=0;
  
  E:
      
      a=rand()%10;
      b=rand()%10;
      c=rand()%10;
      
      i++;  
    
      
      cout<<" i="<< i <<"  -  "<<a<<"+"<<b<<"-"<<c<<"="<<a+b-c<<endl;
        
      if( i < x ){     goto E;  }  
      if( (a+b-c) > 10  ){  x++; }  else if ((a+b-c)==10){x--;} //Daca a+b-c=10, realizam cu o iteratie mai putin.
      
      //Sa se contorizeze de cate ori a a fost par
      a%2==0 ? par++:0; cout<<" a a fost par de "<<par<<endl;
      //Variabila c primeste doar valori impare
      c%2!=0 ? impar++:0; cout <<" c a fost impar de "<<impar<<endl;
      // Cate rezultate pare si cate rezultate impare
      if ((a+b-c)%2==0){ rez_pare++;}
      else { rez_impare++;}
	cout<<" a+b-c a avut rezultate pare de "<<rez_pare<<" si rezultate impare de "<<rez_impare<<endl;
	   
	  return 0;
  }
Last edited on
Hello MaxGreen,

To start with:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Then your code is very hard to read. I realize that you are just getting started, so this should help.
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    //srand(time(0));
    srand(static_cast<unsigned int>(time(nullptr)));

    int a{}, b{}, c{}, i = 0, x = 10, par = 0, impar = 0, rez_pare = 0, rez_impare = 0;

E:

    a = rand() % 10 + 1;  // <--- Returns numbers 1 - 10.
    b = rand() % 10;      // <--- Returns numbers 0 - 9.
    c = rand() % 10;

    i++;

    cout << " i=" << i << " - " << a << "+" << b << "-" << c << "=" << a + b - c << endl;

    if (i < x)
    { 
        goto E;
    }

    if ((a + b - c) > 10)
    {
        x++;
    }
    else if ((a + b - c) == 10)
    {
        x--;
    } //Daca a+b-c=10, realizam cu o iteratie mai putin.

    //Sa se contorizeze de cate ori a a fost par
    a % 2 == 0 ? par++ : 0; cout << " a a fost par de " << par << endl;

    //Variabila c primeste doar valori impare
    c % 2 != 0 ? impar++ : 0; cout << " c a fost impar de " << impar << endl;

    // Cate rezultate pare si cate rezultate impare
    if ((a + b - c) % 2 == 0)
    {
        rez_pare++;
    }
    else
    {
        rez_impare++;
    }

    cout << " a+b-c a avut rezultate pare de " << rez_pare << " si rezultate impare de " << rez_impare << endl;

    return 0;
}

I have not looked at everything in the code yet, but this should help get you started.

Your first goal is to make your code as easy to read and follow as you can. The first benefit is to you.

Try to avoid using "goto" if you can. This is an unconditional jump that can cause problems in later programs. Most times this can be replaced with a for loop, do/while loop or while loop that will work better that "goto".

Some of this program I will have to translate to understand what it is meaning. It would help if you mention what language it is in.

Andy
It appears to be Romanian...
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    
srand(time(0));

int a,b,c, i=0, x=10, par=0, impar=0, rez_pare=0, rez_impare=0;

//E:

while( i < x) {
    
  a=rand()%10;
  b=rand()%10;
  c=rand()%10;

  i++;

  if (a+b-c > 10) {
    x++;
  }
  if (a+b-c == 10) {
    x--;
  }
  if (a%2 == 0) {
      par++;
  }
  if (c%2 != 0) {
      impar++;
  }
  if ((a+b-c)%2 == 0) {
      rez_pare++;
  }
  else {
    rez_impare++;  
  }

cout<<" i="<< i <<" - "<<a<<"+"<<b<<"-"<<c<<"="<<a+b-c<<endl;

}

cout << "a was even " << par << " times." << endl;
cout << "c was odd " << impar << " times." << endl;
cout << "result was even " << rez_pare << " times." << endl;
cout << "result was odd " << rez_impare << " times." << endl;

return 0;
}
@Handy Andy, Manga, thank you for your advice and all the help provided.
@manga, we didn't learn the "while " function yet. However when i read your written code it makes more sense to use it instead of goto.

Can you explain shortly how the "while" function should be used?!

Thanks in advance! I am new on the website and forum and hope to step in asap.

That's unfortunate that you haven't learned the while() loop yet. You should learn it before learning goto. Goto is bad form. Scroll down to the while loop description on this page:
http://www.cplusplus.com/doc/tutorial/control/
Last edited on
Topic archived. No new replies allowed.