Author Topic: Statements in while loop don't work well  (Read 1702 times)

0 Members and 1 Guest are viewing this topic.

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Statements in while loop don't work well
« on: December 03, 2013, 05:47:38 pm »
Hello again, I fkced up once more. So,  I have one while loop that will last forever (basicly last forever until user want to leave application) . And for example, if user enter 1, goto will put him back to while loop and trigger all this again. BUT, problem is that when that happen, somehow the code when user need to enter value is just passed and I don't want that to happen. Here is code:

Code: [Select]
#include <cstdlib>
#include <iostream>

using namespace std;
void Razvrstaj(int& a,int& b,string recenica);
int main(int argc, char *argv[])
{
    Start:
    while(1) {         
    string Recenica;
    int Suglasnik = 0,Samoglasnik = 0;
    cout << "Unesite Neku recenicu:" << endl;
    getline(cin,Recenica);
    Razvrstaj(Samoglasnik,Suglasnik,Recenica);
    cout << "Ukupan broj samoglasnika je : " << Samoglasnik << endl << "Ukupan broj suglasnika je: " << Suglasnik << endl;
    cout << endl << "Da li zelite uneti novu rec? (1-da/0-ne)" << endl;
    int vrednost;
    cin >> vrednost;
    if (vrednost == 0) goto Kraj;
    if (vrednost == 1) goto Start;
}
Kraj:
    return EXIT_SUCCESS;
   
}
void Razvrstaj(int& a, int& b,string recenica) {
   int duzina = recenica.length();
   int razmak = 0;
    for (int i = 0; i < duzina ; i++) if (isspace(recenica[i])) razmak++;
   for (int i = 0; i < duzina ; i++) {
       if( (recenica[i] == 'a') || (recenica[i] =='A') || (recenica[i] == 'E') || (recenica[i] == 'e') || (recenica[i] == 'I') ||
       (recenica[i] == 'i') || (recenica[i] == 'O') || (recenica[i] == 'o') || (recenica[i] == 'U') || (recenica[i] == 'u')) {
               a++;     
                    }
       else {
            b++;
            }
       }
       b -= razmak;
}
Vae Victis - suffering to the conquered

Offline Traitor4000

  • Knight
  • **
  • Posts: 191
  • Cookies: 8
    • View Profile
Re: Statements in while loop don't work well
« Reply #1 on: December 03, 2013, 06:02:24 pm »
I was like hey i might get to help someone success, then i looked at the code and it was C++ and i was like damn it  :P i can understand most of it but im probably not the most qualified to help you however the way you accomplished the infinite while does seem a bit odd although that might just be because im comparing this to C.
« Last Edit: December 03, 2013, 06:02:43 pm by Traitor4000 »
The most vulnerable part of an impenetrable system is those who believe it to be so.

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: Statements in while loop don't work well
« Reply #2 on: December 03, 2013, 06:30:33 pm »
Do not use goto's like that... I fixed the code so it doesn't use goto's, but haven't tested it.

Code: [Select]
#include <cstdlib>
#include <iostream>

using namespace std;
void Razvrstaj(int& a,int& b,string recenica);
int main(int argc, char *argv[])
{
    while(vrednost != 0) {         
       string Recenica;
       int Suglasnik = 0,Samoglasnik = 0;
       cout << "Unesite Neku recenicu:" << endl;
       getline(cin,Recenica);
       Razvrstaj(Samoglasnik,Suglasnik,Recenica);
       cout << "Ukupan broj samoglasnika je : " << Samoglasnik << endl << "Ukupan broj suglasnika je: " << Suglasnik << endl;
       cout << endl << "Da li zelite uneti novu rec? (1-da/0-ne)" << endl;
       int vrednost;
       cin >> vrednost;
   }

    return EXIT_SUCCESS;
}

void Razvrstaj(int& a, int& b,string recenica) {
      int duzina = recenica.length();
      int razmak = 0;
      for (int i = 0; i < duzina; i++)
      if (isspace(recenica[i])) razmak++;
      for (int i = 0; i < duzina ; i++) {
          if( (recenica[i] == 'a') || (recenica[i] =='A') || (recenica[i] == 'E') || (recenica[i] == 'e') || (recenica[i] == 'I') ||
             (recenica[i] == 'i') || (recenica[i] == 'O') || (recenica[i] == 'o') || (recenica[i] == 'U') || (recenica[i] == 'u')) {
               a++;
        }
       else {
            b++;
       }
      }
     b -= razmak;
}
« Last Edit: December 03, 2013, 06:31:30 pm by s3my0n »
Easter egg in all *nix systems: E(){ E|E& };E

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: Statements in while loop don't work well
« Reply #3 on: December 03, 2013, 07:16:53 pm »
I tested this and still same problem. =/
Vae Victis - suffering to the conquered

Offline vezzy

  • Royal Highness
  • ****
  • Posts: 771
  • Cookies: 172
    • View Profile
Re: Statements in while loop don't work well
« Reply #4 on: December 03, 2013, 07:19:32 pm »
Do not use goto's like that... I fixed the code so it doesn't use goto's, but haven't tested it.

Actually, his use of gotos in this case are perfectly decent. He's using them to break out of loops and head to an explicitly labeled exception (EXIT_SUCCESS, in this case).

I'm not sure if they're exactly the best for a short code snippet like this, but in general there's nothing wrong.
Quote from: Dippy hippy
Just brushing though. I will be semi active mainly came to find a HQ botnet, like THOR or just any p2p botnet

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Statements in while loop don't work well
« Reply #5 on: December 03, 2013, 07:45:31 pm »
I never really used goto, but returning from goto doesn't look like it's affecting the function that's calling it? It looks like the goto is acting like a function and returning from itself instead of main returning.

In any case, it looks like main isn't returning anything and the while(1) continues, the goto seems to be returning to main not from it. main should either return itself or use a diff conditional or "break" if that's valid. Why not use the vrednos var as the condition for the loop? But, then again I don't do enough C++ to say other wise.

Also, since I don't speak your language it looks confusing anyway lol. But code is code I guess lol.
« Last Edit: December 03, 2013, 07:56:21 pm by techb »
>>>import this
-----------------------------

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: Statements in while loop don't work well
« Reply #6 on: December 03, 2013, 08:01:16 pm »
I tried also to make vrednost to be condition in while loop, and after i change value to exit loop or continue loop. But still same problem happen. While loop start all over and simply ignore the fact I entered there getline(cin,Recenica); He simple pass over it and come to last question. Don't get it why.
Vae Victis - suffering to the conquered

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Statements in while loop don't work well
« Reply #7 on: December 03, 2013, 08:04:45 pm »
I tried also to make vrednost to be condition in while loop, and after i change value to exit loop or continue loop. But still same problem happen. While loop start all over and simply ignore the fact I entered there getline(cin,Recenica); He simple pass over it and come to last question. Don't get it why.

Have you tried declaring verdnos outside of the while loop? I'm on my phone and can't compile and run it.
>>>import this
-----------------------------

Offline Code.Illusionist

  • Royal Highness
  • ****
  • Posts: 687
  • Cookies: 39
  • Compile or die trying
    • View Profile
Re: Statements in while loop don't work well
« Reply #8 on: December 03, 2013, 08:06:40 pm »
Yes, I declared that variable before while loop because it wouldn't have sense to be declared inside loop. If it's condition, the condition will be checked before even coming to declaration, therefor I declared and set value out of while loop (before) . It's confusing for me . :D


I solved it, it seems I had to clear buffer after using cin . =) Code:


Code: [Select]
#include <cstdlib>
#include <iostream>

using namespace std;
void Razvrstaj(int& a,int& b,string recenica);
void UzmiRecenicu(string& recenica);
int main(int argc, char *argv[])
{     
       int vrednost;
       do  {
       string Recenica;
       int Suglasnik = 0,Samoglasnik = 0;
       UzmiRecenicu(Recenica);
       Razvrstaj(Samoglasnik,Suglasnik,Recenica);
       cout << "Ukupan broj samoglasnika je : " << Samoglasnik << endl << "Ukupan broj suglasnika je: " << Suglasnik << endl;
       cout << endl << "Da li zelite uneti novu rec? (1-da/0-ne)" << endl;
       cin >> vrednost;
       cin.ignore();
       if (vrednost == 0) goto KRAJ;
       } while(vrednost == 1);
       KRAJ:
       return EXIT_SUCCESS;
}

void Razvrstaj(int& a, int& b,string recenica) {
      int duzina = recenica.length();
      int razmak = 0;
      for (int i = 0; i < duzina; i++)
      if (isspace(recenica[i])) razmak++;
      for (int i = 0; i < duzina ; i++) {
          if( (recenica[i] == 'a') || (recenica[i] =='A') || (recenica[i] == 'E') || (recenica[i] == 'e') || (recenica[i] == 'I') ||
             (recenica[i] == 'i') || (recenica[i] == 'O') || (recenica[i] == 'o') || (recenica[i] == 'U') || (recenica[i] == 'u')) {
               a++;
        }
       else {
            b++;
       }
      }
     b -= razmak;
}
void UzmiRecenicu(string& recenica) {
     cout << "Unesite neku recenicu:" << endl;
     getline(cin,recenica);
     }

Just in case someone need it =)
« Last Edit: December 03, 2013, 09:42:04 pm by Code.Illusionist »
Vae Victis - suffering to the conquered

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Statements in while loop don't work well
« Reply #9 on: December 04, 2013, 12:27:33 pm »
Now that I'm not on my phone and on my computer now, I compiled the original and it worked fine for me.

Code: [Select]
g++ -o test test.cppRan it, and it worked as expected, so I guess it also depends on compilers I guess.

At least you found a solution.
>>>import this
-----------------------------

Offline ArkPhaze

  • Peasant
  • *
  • Posts: 136
  • Cookies: 20
  • null terminated
    • View Profile
Re: Statements in while loop don't work well
« Reply #10 on: December 16, 2013, 07:00:19 am »
Actually, his use of gotos in this case are perfectly decent. He's using them to break out of loops and head to an explicitly labeled exception (EXIT_SUCCESS, in this case).

I'm not sure if they're exactly the best for a short code snippet like this, but in general there's nothing wrong.

Quite the opposite tbh, it's spaghetti code in his case because instead of just directly calling return EXIT_SUCCESS, or even better yet, using the while loop construct the way it should be used--with a condition that would break out of the loop to return EXIT_SUCCESS. Instead, he's creating an infinity loop, and complicating the code with a goto that jumps to that location instead.

Things that would've been better before resorting to a goto in his case:
1. break;
2. Using the condition within the while loop to be proper with what the while loop was designed for

The only real use a goto has, is if you're trying to break out of a nested mess. Aside from that, you should try to avoid code that is heavily nested in the first place, but if you can't, that is the 1% of the time where a goto is perfectly acceptable. I'm not seeing the requirement for a goto based on what he's trying to do though.

edit: Btw, a tip, there exists many methods within the STL that can be used to actually make use of what C++ provides OP. For instance:
Code: [Select]
int razmak = std::count_if(recenica.begin(), recenica.end(), [](char c) { return isspace(c); });
Would make that for loop redundant. It comes from the <algorithm> header.

Code: [Select]
#include <algorithm>
And don't forget about iterators.
« Last Edit: December 16, 2013, 07:26:26 am by ArkPhaze »
sig=: ArkPhaze

[ J/ASM/.NET/C/C++ - Software Engineer ]