Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

faulty dog

Status
Not open for further replies.

benthejack

Programmer
Dec 3, 2004
2
NZ
hi i just started c++ yesterday (hence this is a rather remedial question) and have been following an internet tutorial.
anyway i finished the tutorial and decided to test my current knowledge by writing a basic program which asks if you can spell dog (hehe). but once i finished i found it was slightly faulty because it would always be correct if the word started with d
ie.. if you spelt dog dkg it would return positive
but if the first letter wasnt d ie you spelt it fog it would say it was wrong.
the code i used is below:

#include <iostream.h>
#include <stdlib.h>

long int wait;
int letter1(char &in1);
int letter2(char &in2);
int letter3(char &in3);

int main()
{
int X,Y,Z;
char A,B,C;
cout<<"can you spell dog??\n";
cin>>A>>B>>C;
//links to other functions
X=letter1(A);

Y=letter2(B);

Z=letter3(C);

if(X==1&&Y==1&&Z==1)
{
cout<<"yes!\n";
for(wait=0;wait<=2000000000;wait++)
{for(wait=0;wait<=2000000000;wait++)
{for(wait=0;wait<=2000000000;wait++)
{for(wait=0;wait<=2000000000;wait++)
{for(wait=0;wait<=2000000000;wait++){}}}}}
cout<<"\n";
main();
}
else
{
cout<<"no\n";
for(wait=0;wait<=2000000000;wait++)
{for(wait=0;wait<=2000000000;wait++)
{for(wait=0;wait<=2000000000;wait++)
{for(wait=0;wait<=2000000000;wait++)
{for(wait=0;wait<=2000000000;wait++){}}}}}
cout<<"\n";
main();
}
}

int letter1(char &in1)
{
switch(in1)
{
case 'd': case 'D':
return 1;
}
}

int letter2(char &in2)
{
switch(in2)
{
case 'o': case 'O':
return 1;
}
}

int letter3(char &in3)
{
switch(in3)
{
case 'g': case 'G':
return 1;
}
}
 
Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

One of your functions
Code:
int letter3(char &in3)
{
  switch(in3)
  {
    case 'g': case 'G':
    return 1;
  }
  /* if the letter isn't a 'g' or a 'G' */
  /* return some totally random value which from your */
  /* comments isn't zero */
}

--
 
how do you do a false case return ??
ie.. how do you say if case is not equal to??
 
Your "wait" loops are bad for many many reasons. Use the Windows API Sleep().

Also, there is no need to use a switch when you are only looking at one case, it would be better to just use an if statement.
 
Another thing you might want to do is use parentheses to separate terms more clearly. Ex.
Code:
if( (X==1) && (Y==1) && (Z==1) )
I think it should work the way you expect without the extra parentheses, but this makes sure that it does, and it's a lot more readable.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top