C win/linux trouble
I've recently installed linux on my machine and i have started programming with C in it recently. I've found out some problems since there is no system("PAUSE") commands on linux. I looked up on google and the suggestions of people is to replace system"("PAUSE") by getchar() , but i tried lots of differnt ways and i couldn't make my program simulate that pause. It just simply run as if there was no getchar() there. Do any of you know how could i make that work?
Also, i've heard of commands named sleep() and usleep(). According to the sites i've found, they work setting a time for the program to sleep (quite logic, huh? :P). But then, what none of them really explained, was the difference between sleep and usleep. Does anybody know the difference between them?
spyware wrote: getchar = (waiting for) user input.
I recommend you download an ebook and do some chapters, before venturing in the wondrous world of C alone.
Good luck!
i've used getchar many times before, but it does not act just like a pause. I know it waits for user input, but then, why does it not wait for my input in linux? that is my doubt. I do the same exact thing in windows and it does work. Understad what i mean now?
thank you for answering my doubt with sleep and usleep.
ynori7 wrote: What do you need system('pause') for anyway? In windows you generally use it to prevent your console box from disappearing as soon as the program is finished, but in linux you ought to be running your programs through the console anyway.
And in the future, disable smilies.
Well, the thing is that i wrote the program on windows, and i wanted to try it on linux just to test the compiler, change the code a bit and feel the difference. I will present the win version on university, and i wanted my linux version to do exactly the same as the windows version. No real reason.
Sorry for the smiles…
chess_rock wrote: Well, the thing is that i wrote the program on windows, and i wanted to try it on linux just to test the compiler, change the code a bit and feel the difference. I will present the win version on university, and i wanted my linux version to do exactly the same as the windows version. No real reason. Then just comment out the system("pause") before testing it in linux. It should work fine then unless you're doing anything else in the program that is OS-specific.
ynori7 wrote: Then just comment out the system("pause") before testing it in linux. It should work fine then unless you're doing anything else in the program that is OS-specific.
Yeah, i've done that already. But i'll explain it more clearly… There are many different switches, and at the end of all of those, i tell the program to pause, clear all the terminal, and go back to the main function. I want that pause to make the program go back to the main function with the window clean. I know that in linux, system("CLS") = system("clear"), i just need the pause really. Any suggestions?
chess_rock wrote: [quote]ynori7 wrote: Then just comment out the system("pause") before testing it in linux. It should work fine then unless you're doing anything else in the program that is OS-specific.
Yeah, i've done that already. But i'll explain it more clearly… There are many different switches, and at the end of all of those, i tell the program to pause, clear all the terminal, and go back to the main function. I want that pause to make the program go back to the main function with the window clean. I know that in linux, system("CLS") = system("clear"), i just need the pause really. Any suggestions?[/quote]
I think I might be confused about what your trying to do. I may not be a C expert, but in python (in linux), pausing and clearing the screen and running the main loop is as easy as:
os.system("sleep 5")
# sleep for 5 seconds
os.system("clear")
# clear the screen
break
# exit this loop or if statement or whatever```
or, if I was waiting for user input:
```markup
raw_input("press enter")
os.system("clear")
break```
I could be completely misunderstanding you, but something like that shouldnt be that hard to implement in C
COM sent me a pm to help me with this and it was very useful. There were two ideas in it:
- Avoid system(), since different systems have different commands.
- Try using cin.get()
For 1), i understand the problem of compatibility and i think this is a very valuable information.
I tried 2) and it didn't work, so i just resigned making my program similar to that of windows, especially because it will work at the terminal window.
I will keep programming in windows as long as i need to display things orderly, with pauses and CLS. Much easier than trying to re-adapt the whole thing.
I'm posting this conclusions here as to close this thread and in case any person has the same problem that i had: this could turn out to be useful.
chess_rock wrote: COM sent me a pm to help me with this and it was very useful. There were two ideas in it:
- Avoid system(), since different systems have different commands.
- Try using cin.get()
For 1), i understand the problem of compatibility and i think this is a very valuable information.
I tried 2) and it didn't work, so i just resigned making my program similar to that of windows, especially because it will work at the terminal window.
I will keep programming in windows as long as i need to display things orderly, with pauses and CLS. Much easier than trying to re-adapt the whole thing.
I'm posting this conclusions here as to close this thread and in case any person has the same problem that i had: this could turn out to be useful.
Seems you misunderstood my PM a bit. At point one, I listed three reasons for not using system(), but let's leave that as that was not the main issue. I did not suggest you to use cin.get() as that is C++, while strictly C does work with C++, it does not work the other way around. What I did was liken it to an issue that I recognize from using C++. The issue in question is that when you've already input something once, the return value is still in the in buffer. What this leads to is that when you later on call a function which is content with just taking a return value, it will consume the one in the in buffer and move on. To make it pause you will just have to use the same command twice, in this case:
getchar();
getchar();
This should make the return value disappear and then proceed to waiting for input; which in this case, as wanted, can be only enter. Hope it helps, I was really hoping to avoid having to post here myself… sigh.
COM wrote: [quote]chess_rock wrote: COM sent me a pm to help me with this and it was very useful. There were two ideas in it:
- Avoid system(), since different systems have different commands.
- Try using cin.get()
For 1), i understand the problem of compatibility and i think this is a very valuable information.
I tried 2) and it didn't work, so i just resigned making my program similar to that of windows, especially because it will work at the terminal window.
I will keep programming in windows as long as i need to display things orderly, with pauses and CLS. Much easier than trying to re-adapt the whole thing.
I'm posting this conclusions here as to close this thread and in case any person has the same problem that i had: this could turn out to be useful.
Seems you misunderstood my PM a bit. At point one, I listed three reasons for not using system(), but let's leave that as that was not the main issue. I did not suggest you to use cin.get() as that is C++, while strictly C does work with C++, it does not work the other way around. What I did was liken it to an issue that I recognize from using C++. The issue in question is that when you've already input something once, the return value is still in the in buffer. What this leads to is that when you later on call a function which is content with just taking a return value, it will consume the one in the in buffer and move on. To make it pause you will just have to use the same command twice, in this case:
getchar();
getchar();
This should make the return value disappear and then proceed to waiting for input; which in this case, as wanted, can be only enter. Hope it helps, I was really hoping to avoid having to post here myself… sigh.[/quote]
See his avatar? Thats what he's doing upon reading that.
COM wrote: [quote]chess_rock wrote: COM sent me a pm to help me with this and it was very useful. There were two ideas in it:
- Avoid system(), since different systems have different commands.
- Try using cin.get()
For 1), i understand the problem of compatibility and i think this is a very valuable information.
I tried 2) and it didn't work, so i just resigned making my program similar to that of windows, especially because it will work at the terminal window.
I will keep programming in windows as long as i need to display things orderly, with pauses and CLS. Much easier than trying to re-adapt the whole thing.
I'm posting this conclusions here as to close this thread and in case any person has the same problem that i had: this could turn out to be useful.
Seems you misunderstood my PM a bit. At point one, I listed three reasons for not using system(), but let's leave that as that was not the main issue. I did not suggest you to use cin.get() as that is C++, while strictly C does work with C++, it does not work the other way around. What I did was liken it to an issue that I recognize from using C++. The issue in question is that when you've already input something once, the return value is still in the in buffer. What this leads to is that when you later on call a function which is content with just taking a return value, it will consume the one in the in buffer and move on. To make it pause you will just have to use the same command twice, in this case:
getchar();
getchar();
This should make the return value disappear and then proceed to waiting for input; which in this case, as wanted, can be only enter. Hope it helps, I was really hoping to avoid having to post here myself… sigh.[/quote]
See his avatar? Thats what he's doing upon reading that.
varreon wrote: Are you writing C or C++? The cin is an object from the C++ libraries. Using the methods declared in stdio.h(standard C library), you could use the following function:
void pause(void){ char c; printf("Hit return to continue…\n"); scanf("%c",&c); }
and of course, call it with pause();
Unnecessary. Not only did I explain the issue two posts ago and said that he had gotten it wrong as cin is C++ and not C, but that code will in fact not work if something besides only enter has been input by the user beforehand.
oops! You're right, cin.get() is a c++ command. I've been programming C for a very short time, and i sometimes mix the commands with C++, which i've studied few, a long time ago. I'm not in touch with these languages all the time, since i'm more into PHP lately. And coming back to them brings a small confusion sometimes.
I've realized it is not possible to make C programs work the same on windows and linux, since both run the programs differently. The most similar to a pause would be a simple getchar().
I've tried read -n1 and it did not work properly as i would like it to work. And for scanf, i needed to create an extra variable, which i don't like much. My program might run under a very slow computer, imagine something a lot worse than a gameboy. So i don't like creating extra variables.
Sorry for making you post here COM, i didn't realize cin.get was a c++ command when you told me.
By the way, i didn't like the system("clear") a lot… It is better to load the program again and again rather than the "cute" look i tried to give to the program. I've realized that if i want my program to look "cute", i should program it on windows, no linux. Thank you all for your suggestions. I've tried them all :)
EDIT: With two getchar() the program didn't just jump the code as if there was nothing there. It stopped, waiting for my input.