EvilZone
Programming and Scripting => C - C++ => Topic started by: Code.Illusionist 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:
#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;
}
-
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.
-
Do not use goto's like that... I fixed the code so it doesn't use goto's, but haven't tested it.
#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;
}
-
I tested this and still same problem. =/
-
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.
-
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.
-
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.
-
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.
-
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:
#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 =)
-
Now that I'm not on my phone and on my computer now, I compiled the original and it worked fine for me.
g++ -o test test.cpp
Ran it, and it worked as expected, so I guess it also depends on compilers I guess.
At least you found a solution.
-
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:
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.
#include <algorithm>
And don't forget about iterators.