can't find why this bug occur
Apr 5, 2019 at 3:53pm UTC
in my function i like to take a char from the user but that char must be small letter and same letter cannot be used twice
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
#include <iostream>
#include <string>
using namespace std;
char letter(string &);
int main()
{ //dont look at this i just randomly tasting
string alo=" " ; char a;
for (int i=0;i<3;i++){
a= letter(alo);}
cout<<a<<" string " <<alo;
return 0;
}
char letter(string &alo){
char a;bool ok=true ;
while (ok){
cin>>a;
for (int i=0;i<alo.size();i++){
if (a>='a' &&a<='z' &&a!=alo[i]){ //here a!=alo[i] is ignored
alo+=a;
ok=false ;
return a;
}
else
cout<<"bad input" ;
}}
}
Apr 5, 2019 at 4:13pm UTC
Line 21: The first char of alo is a space, so a!=alo[i] is always going to be true the first time through the loop. That's always going to cause you to return a at line 24 the first time through the loop.
Topic archived. No new replies allowed.