Archived

This topic is now archived and is closed to further replies.

Trouble with code...

This topic is 5531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This code seems simple enough.. I am having trouble when entering anything to this function.. It could be this function (even though I see nothing wrong with it!). Or it could be what it''s going to? Can someone help.. I have the debug info if you would like.. void myloop(void) { int i = 0; while(i != 1 || i != 2) { printf("\nHi, welcome..\n\n"); printf("1.) Enter Game\n"); printf("2.) Exit Game\n"); printf("Your choice[1 or 2]: "); scanf("%d",i); if(i = 1) { gameloop(); } if(i = 2) { return; } printf("Try again..\n"); } }

Share this post


Link to post
Share on other sites
scanf expects a pointer to the target variable

= is an assignment, == is a test.

( i = 1 ) evaluates to 1, which is true
( i = 2 ) evaluates to 2, which is true

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
I changed the code to look like this... but it still wont work for me...

void myloop(void)
{
int i;

while(i != 1 || i != 2)
{
printf("\nHi, welcome..\n\n");
printf("1.) Enter Game\n");
printf("2.) Exit Game\n");
printf("Your choice[1 or 2]: ");
scanf("%d",&i);
switch (i)
{
case 1:
{
gameloop();
}
case 2:
{
return;
}
printf("Try again..\n");
}
}
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Look at the truth table for ''i != 1 || i != 2''. If i is equal to 1, then the statement is true for then it is not equal to 2. Contrariwise, if i is equal to 2 then the statement is true because i is not equal to 1. If it is equal to neither 1 nor 2, then the statement is doubly true.

Your switch statement also has a problem. A ''break;'' statement must seperate each clause or the program will execute every clause.

Also, you should use just one method for your loop/break mechanism. Either make the loop conditional upon a 2 -- while (i != 2){} -- in which case do nothing in the case of a 2 being entered for it will automatically break out of the loop that iteration. Or make it an explicit infinite loop so that the reader expects a break or return in the middle of the loop -- while (true){} .

-D

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
One other thing I just noticed. Before your ''try again'' printf statement, you need to explicitly notify the compiler that it is the default case by typing ''default:'' just before it.

-D

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Melraidin, as much as I dislike causing turmoil, I feel that you may be mistaken in this case. Since Jimmyp4ge is using the ''%d'' field specifier in their scanf statement, the data is translated to an integer type before being stored in i. Therefore it is necessary to check for numbers rather than symbols.

Jimmyp4ge, you initialized i to 0 in your first code fragment which was good. However, you forgot to initialize it in your second. Be sure that the code you are trying to run initializes i. Otherwise undefined behavior may result.

-D

Share this post


Link to post
Share on other sites
Hey, you totally blew my mind... yea, that truth table didn''t work.. buuuutt... still havin trouble.. this thing is crazy.. it looks so simple!

void myloop(void)
{
int i = 0;

while(true)
{
printf("\nHi, welcome..\n\n");
printf("1.) Enter Game\n");
printf("2.) Exit Game\n");
printf("Your choice[1 or 2]: ");
scanf("%d",i);
switch (i)
{
case 1:
{
gameloop();
break;
}

case 2:
{
return;
break;
}

default:
{
printf("Try again..\n");
}

}

}
}

Share this post


Link to post
Share on other sites
scanf("%d",&i); and
scanf("%d",i);

it dosent make a difference either way.. it''s still getting stuck.. (Damn! If I can''t do this how am I ever going to become a better programmer?)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The &i definitely makes a difference. The problem is likely to be within your gameloop() function. Try replacing it with a ''printf("gameloop stuff\n";'' and check to make sure that the problem is really in the myloop() function.

btw, don''t worry about coming across troubles. The only way that people learn programming so that it soaks into their bones is by spending hours bashing into problems until they figure out a way around it. This is because if you spend hours figuring something out, you will likely never forget it.

-D

Share this post


Link to post
Share on other sites
You nailed it.. I replaced the gameloop() with printf and it works fine! Now, im afraid to open this whole new can of worms..

void gameloop(void)
{
char* command = NULL;
while(strcmp(command, "x" ) != 0)
{
current.gettitle();
current.getexits();
scanf("%s",&command);
move(command);
initialize();
}
}

current.gettitle() and getexits() is just a function with access to the private data of the class and it just prints a char*.

void gettitle(void)
{
printf("%s",title);
}

void getdesc(void)
{
printf("%s",desc);
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

  
void gameloop(void)
{
static const size_t MAX_COMMAND_SIZE = 255; // tweakable

char command[MAX_COMMAND_SIZE];
command[0] = ''\0''; // command is now a null string

while(strcmp(command, "x" ) != 0) // ok

{
current.gettitle(); // consider your naming scheme

current.getexits(); // you may want to rename these

// to ''print*'' or ''display*'' to establish that they

// output to the screen rather than to other parts of the

// program.

scanf("%s",command); // don''t put an ampersand here because

// command is already a pointer to a string.

move(command); // ok

initialize(); // why is initialize at the end?

}
}


When using c-style strings, it is important to differentiate between a ''NULL pointer'' and a ''pointer to a null string''. The first is a pointer that specifically doesn''t point to anything. The second is a pointer that points to a string (which happens to be empty).

Note that the characteristic that determines whether an array of chars is a string or not is simply whether or not there is a null character ''\0'' denoting when the list of valid characters stops. This means that every string must have an extra character at the end to denote the end of the string.

You may wish to consider using the STL string and iostream classes for input/ouput and text processing. They are generally a lot more intuitive. Most importantly, they make it so that you don''t have to worry about subtle distinctions about pointers and how they work. Pointers are properly learned only later on.

-D

Share this post


Link to post
Share on other sites
Oh lord... that was embarassing... I''m so used to reading into a char variable (or perhaps std::string) that I forgot about scanf''s into chars... My fault. You''re right Anon.

Share this post


Link to post
Share on other sites
move(command) attempts to move my x,y coordinates depending on if the user enters, "north","northeast","east",etc.." my map is just a pre-initialized 2 dimensional class array(ie: room[x][y]).. I have most of my code figured out and changed but I am having 2 problems.. Copying an object to an object.. I know I have to use a copy constructor since I have char*''s I need copied. So the conventional:

room map[5][5];
room current;
....
//do my rooms initialization and later on do this
void initialize(void)
{current = map[x][y];}

A huge mistake thinking I could copy pointers.. they would both point to the same address right? My second problem is.. Well.. passing strings to my class.. I am using this unworking method:

public:
void settext(char* a,char* b)
{
strcpy(title,a);
strcpy(desc,b);
}

title and desc are private char*''s in my class.. I pass the char*''s like so..
map[0][0].settext("Southwest Corner of Town\n","This is a dead end..\n");
Dosen''t seem to be working.. ugh...
Anyway, Anonymous Poster, you have been an extreme help, I really appreciate it.

Share this post


Link to post
Share on other sites