Advertisement

Linked List Question?

Started by April 04, 2002 12:12 AM
3 comments, last by RickyPiller 22 years, 8 months ago
#include "stdio.h" #include "stdlib.h" struct house{ int num_baths; int num_bedrooms; struct house* next; }; typedef house HOME, *PTR_HOME; int main(void) { PTR_HOME home; PTR_HOME head = NULL; char input; int count = 0; while((input = getchar()) != ''x'') { home = (PTR_HOME) malloc(sizeof(HOME)); home->next = head; head = home; home->num_baths = rand()%2; home->num_bedrooms = rand()%4; count++; } system("cls"); printf("%d elements\n", count); home = head; while(home != NULL) { printf("%d, %d\n", home->num_baths, home->num_bedrooms); home = home->next; } scanf("%c", &input); return 0; } This seems to work but the problem is the console kills right away. It doesn''t wait for input on that last scanf() like it should. I just use that last scanf() so I can keep the screen up. Any idea why? **************************** If religious DONOT read **************************** Jesus was the best skam artist of all times.
****************************If religious DONOT read****************************Jesus was the best skam artist of all times.
Whenever you do a scanf with the %c specifier and it doesn''t prompt you like it should, this means that there was still unread data on the input stream. I''m not sure, but in your situation, I am thinking that the getchar() towards the top is leaving a carriage return on the input stream and scanf is just picking up that carriage return, so it doesn''t need to prompt you, because it already has one char worth of data. If you are just trying to freeze the screen, why not use a getch()...If anyone disagree''s please post to correct me, as I''d rather learn and get a bruised ego, then go on thinking incorrectly.


- Free Your Mind -
- Free Your Mind -
Advertisement
I usually use

while(!kbhit())

to pause the screen.. (at the end) or

void WaitForKey() // could inline..
{
while(!kbhit());
getche();
}

anywhere else. Don''t trust my ability to program though; this might be one of the worse ways to do this (I''ve never been told that it was).

why not have a look at std::list<>? it might easier to implementate

-Ron
I''ve seen the gametutorials.com tutorials use "system("pause"; while including <cstdlib>

This topic is closed to new replies.

Advertisement