Forums » Off-Topic

Need some programming help...

12»
Oct 02, 2003 roguelazer link
I figured I'd ask here so FM didn't have to move it. :D I've got a program here with multiple program files, containing various functions each. At the end of one of these functions in a far off file (let's call it number2.c), a call is made to main(), which is in main.c. I want number2.c to wait for the user to press enter before going back to main.c, but I don't know how. I've tried using a getchar(), but it doesn't do anything. Strangely enough, it does work if the getchar is somewhere in main.c, though. Any ideas?
Oct 02, 2003 cembandit link
what os are you using


main()
remote()

main() calls remote() and the remote() runs and returns to main(). main is not generaly called. Try not to get hung up on the files, but rather the functions themselves. You can set the prototypes and defs for a whole slew of related functions in one file. {EDIT in c++ definitions are in cpps, prototypes in headers (.h)} Are you trying to like

A.Main runs, calls remote()
B. Remote function runs, loop it untill user input is right
c. Back in main

if so, thats a normal course if events, no reason to call main, the function will return to main on its own.



You could also loop the remote IN main like this, but I wouldnt, cause result could return something much more usefull then a char, hehe


int main()
{

//I use 0 as a defualt init for chars, personal habit
char result = '0';

//13 is the ascii for return i think,
//in deci form...this really depends on your iostream....
while(! (result == 13) )
{
//do something
result = remote();
}



return 0;
}


char remote()
{
char result= '0';

//outputspam
//userinputloop
//outputspam

return result
}


Its important to note that in windows OOP, its user event driven.
Theres a message loop for messages from the user to the program, like mouse clicks, chars, etc. There are many defined functions and wrapper classes that are a great help in translating the messages, and with MFC the skelliton is all set up. In visual c++ I higly recomend using a console app with mfc support for learning c++, then moving on to a skelltion windows app for examing the message loops. After a while you will desire quick front ends, thats where the Dialog skellitons are handy, but they make a lot of the work invisable so I dont recomend them to learn on. The also got the added complexity of resource files and maps for things like static texts. Sams Publishing is a great book maker, but their visual c++ focuses heavily on MFC. I really like some MFC things, the doc-view system is more for the real world though and not for games. For graphics in windows (non directx), use the win32 api and not the mfc, as you'll be working with handles instead of expensive wrapper objects. If your intrested in making games, pick up some good books on directx, get the directx sdk from microsoft(free), install it after installing visual studios. Youll then be set to follow the books examples. For opengl there is the blue and red books, free versions online. OpenGL is the way to go for linux/mac whatver users as its platform independent.
Oct 02, 2003 Icarus link
I did notice you call main() alot to return from a function when i looked at ya jmaths proggy... I thought it was the "new thing" todo, but really i guess you should be using "return" or nothing at all if its prototyped as a void. I have had probs with getchar() in the past, try another input method like scanf() or fgets() .. see if they will block... Off the top of my head, it could be that the compiler is "optimising" the code and stripping out the getchar() cos it thinks it ain't needed (only a possibility)...


PS: i'm not here.... ;-)
Oct 02, 2003 roguelazer link
But it's more like this:

main() calls select1()
In select1, user selects from various remote() programs
remote() runs and then, when finished, returns control to main(), not to select1()



Main()---->Select1()-------->Remote()
^________________________________|


This is under linux.

As for scanf() and fgets(), those require that you enter a NON-enter character, then enter.
Oct 02, 2003 Icarus link
Personally i'd never jump back like that... i'd return through the functions.... try making sure you load an int with the getchar() and do something with it ( * 2) or something, just to make sure its not getting taken out...
Oct 02, 2003 furball link
If you MUST MUST MUST jump like that (though as Icarus says, I STRONGLY suggest you NOT do this!) then look into the C functions setjmp and longjmp as they are what you want. However, I can't stress enough that this is a BAD idea.

(Note, caps used for emphasis.)
Oct 03, 2003 roguelazer link
Um. So how do I go from Remote() to main() without jumping?
Oct 03, 2003 Zarchun link
You don't go to main, you return to main. It would be helpful if you posted more of what you want to do... There is as good as always a solution without goto/jump. (I'm sure it could be mathematically profen.)

So what exactly do you want to do? Maybe you'll need something like a messageloop or ... Look at Cembandit's example. You always only call remote procedures from main.
Oct 03, 2003 Renegade ++RIP++ link
euhm on the virge of stupidity.

Why dont you just use break ?

it will kick you out of the loop you are in at the moment to the previous loop. And after it you can do a break again , and get kicked back to main.

besides you can always make a double break or test on some variable to see if you entered the loop.

main --> select1() --> "set x to 1" remote() "break" --> compare if x == 1, if so do break else go to the next command.

main
{
-int z;
if (z == 5){
-select1();
--}
}

select1(){
-if (blabla == 1){
--int x = 1;
--remote();
-}
-if (x==1){
--break(); // this will boot you back to main()
-}
-x = 0;
-if (blabla == 2){
--// whatever you were planning
-}
}

remote(){

// your thing you wanted to do, and as long as you didnt complete what you wanted to do, you just dont use the break() call. As soon as you use it , you wil get booted back to select
}


Euhm and if I made some gigantic mistakes, then so be it, im no informatics person :D, I only know some very very very very "did I stress it enough?" basic c++/c/java code

EDIT: I hate that spacing junk :(
Oct 03, 2003 roguelazer link
I have this (in a nutshell)


int Main()
{
int blah;
puts("Enter your choice");
scanf("%d",blah);
if blah == 1
{
Select1();
}
else
Select2();
return 0
}

Select1()
{
int blahblah;
puts("Enter 1 for choice 1, 2 for choice 2 or 3 to go back to main");
scanf("%d",blahblah);
if (blahblah == 1)
{
Choice1();
}
if (blahblah == 2)
{
Choice2();
}
else
{
break();
}
}

int Choice1()
{
blablah
Main()
}








How can I do that, keeping all of them in separate functions (I have a reason for this, don't worry)? I don't really want main to contain all 10 of the select functions, because I don't like 500 line main()'s.
Oct 03, 2003 Icarus link
ok, heres what i'd do given that code (flame as you see fit) i don't claim i know howto code "properly";


int Main(){
   int blah;
   do{
     puts("Enter your choice");
     scanf("%d",blah);
     if (blah == 1) Select1();
     else if (blah == 2) Select2();
   while(blah != 0)
   return 0
}

void Select1(){
   int blahblah;
   puts("Enter 1 for choice 1, 2 for choice 2 or 3 to go back to main");
   scanf("%d",blahblah);
   if (blahblah == 1) Choice1();
   else if (blahblah == 2) Choice2();
}

void Choice1(){
   blablah
}


No need for "break" or calls to main() at all!

PS: doesn't my code style look better! ;-)
Oct 03, 2003 furball link
Nope! Your coding style is ALL wrong! :D

It OUGHT to be:

int Main()
{
' int blah;
' do
' {
' puts("Enter your choice");
' scanf("%d", blah);
' if (blah == 1)
' Select1();
' else if (blah == 2)
' Select2();
' }
' while (blah != 0) /* You could ALSO do while (blah) */
' return(0);
}

etc. :)

Hmm let's see if THIS does it. (Note, of course remove the ' in front of each line. :))

Nope.... damn board... ok.. after each {, all succeeding lines are indented 4 spaces per level of nesting.
Oct 04, 2003 roguelazer link
Ywah, but doesn't that go back to Select1() after the call to Choice1()?


PS: I found a new problem. It turns out that after the first scanf in Choice1(), NO input is a accepted. I made Choice1() into its own program file with a little main which has 2 likes: Choice1(); return 0;. Any scanf, fgets, getchar, etc after the first scanf in Choice1() is ignored. I Dunno why.
Oct 04, 2003 cembandit link
Wanted to point out that if you declare pointers to variables in a funtion outside of main, and you want to return those pointers or dereferance em latter...make damn sure they are static. You dont want the prog accessing garbage. Static vars are less lame then globals...
Oct 04, 2003 cembandit link
//some copy and pasteable code
// i tried to make it work on linux, but as i dont run linux...
// getinput is prototyped in this file instead of a header
//main only contains 1 real line, the function call
//the rest is fluff for demonstration reasons and can be removed
//note that we are using chars instead of ints
//but you can convert to int or use another iostream function
//gets() is preferable over get(), for saftey of buffer overuns
//if you decide to cast to int, it will turn ascii, a simple
//minus operation or a function from some lib can fix that
//remove all lines with a //rml comment latter to
//clean up main, after you see that main indeed has the info
//from the returned char*
//returning guess is funny since it will always have
// the same value on function exit, but im silly

#include "stdafx.h"
#include "string.h"
char * GetInput();

int main()
{
//lets make a string to hold the data, for fun
//if we remove all the printfs in main we dont
//need this next line
char *mainbuffer = "sometext"; //rml


//call function
mainbuffer = GetInput(); //change line to GetInput(); latter

//we are back in main now, lets show that main has access
//to the data still
printf("Main says: You picked the right number!\n"); //rml
printf("The number was: "); //rml
printf(mainbuffer); //rml
printf("\n"); //rml
return 0;
}



char * GetInput()
{
char *awnser = "103";

//must be static!
static char guess[50];

int correct = 0;

printf("Enter a number: ");

while(!correct) {
gets(guess);
if(strcmp(awnser, guess)==0) {
printf("Correct!\n");
correct = 1;
}
else {
printf("No, try again: ");
}
}

return guess;
}
Oct 04, 2003 cembandit link
//the clean version


#include "stdafx.h"
#include "string.h"
char * GetInput();

int main(int argc, char* argv[])
{
GetInput();
return 0;
}



char * GetInput()
{
char *awnser = "103";
static char guess[50];
int correct = 0;

printf("Enter a number: ");

while(!correct) {
gets(guess);
if(strcmp(awnser, guess)==0) {
printf("Correct!\n");
correct = 1;
}
else {
printf("No, try again: ");
}
}

return guess;
}
Oct 05, 2003 Renegade ++RIP++ link
/me slaps icarus and asks how he did the spacing ?

cheerios
Oct 05, 2003 roguelazer link
Like this? The code for it is this:

& n b s p ;

without the spaces. :P


cembandit: Sorry, but gets is kinda out of the question since a 2-year old can buffer-overrun it.








EDIT: Yay! I made it work with this strange code:

char back;
printf("Perform another calculation?(y/n): ");
scanf("%c%c",back,back);

Strange, eh? It's like the first one is ignored or somethign. I'm gonna go try buffers
Oct 05, 2003 roguelazer link
New results: I tested this code and got a strange result:

float radius = 0.0;
char back = 'm';
puts("Enter the radius:");
scanf("%f",&radius);
puts("Perform another calculation? (y/n)");
scanf("%c",&back);
puts(back);


I entered this:

Enter the radius: 0.0
Perform another calculation? (y/n)
.


It didn't give me a prompt to perform another calculation, and it said back was equal to "." Funky!
Oct 05, 2003 cembandit link
You overan your buffer, and back should be a referance

int main()
{
float radius = 0.0;
char back = 'm';
puts("Enter the radius:");
scanf("%f",&radius);
puts("Perform another calculation? (y/n)");
scanf("%1s",&back);
//putchar(back);
puts(&back);

return 0;
}