He was quoting my original code, which I then changed after he pointed out the little typo.Originally Posted by Apocalypse
He was quoting my original code, which I then changed after he pointed out the little typo.Originally Posted by Apocalypse
Sent from my iPad®
i guess my main question is can you have
a) multiple if statements in a program (im guessin yes)
b) can you have an "if" inside another "if" (not so sure)
any suggestions/answers?
thanks
> can you have more than one getchar in a program?
Yes, yes and yes to your questions.
However, your problem is basically one of mixing getchar() and scanf().
scanf() processes the minimum number of characters necessary to complete your conversion string, and anything else is left for the next input call. This normally isn't a problem if the next call is another scanf call, but for any other input function, the results are surprising.
So, what you would do is something like
Code:int main() { char buff[BUFSIZ]; char q; char a; char original[50]; char orig[50]; char file[10]; char dest[50]; char cp[100]; printf("where is the file you would like to copy? \n" "if the file is on the desktop, type \"y\" \n" "if not, type \"n\" \n"); fgets( buff, sizeof buff, stdin ); q=buff[0]; /* q=getchar(); */ if(q=='y') { printf("now type the file name.\n"); fgets( buff, sizeof buff, stdin ); sscanf( buff, "%s", file); /* scanf("%s", &file); */
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
ok thanks for the help, but that looks way beyond me (me being about half way through c for dummies) so ill wait. i have a few working versions, just without all the bells and whistes i would like. i made one to try to let the user choose between rm, cp, and mv. needless to say it didnt work...ill just have to wait. thanks a lot
There's an FAQ entry on clearing the input buffer: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
i dont realy get that, or what it does or how it helps...why is my program skipping over the if's and getchar's after the first one?
q=getchar();
You type in 'y' and enter.
The 'y' is read by getchar() and the '\n' is left for the next input function to deal with.
scanf("%s", &file);
You type in "file.txt" and press enter.
But there was a '\n' from the previous input there already. This is OK because scanf(%s) skips leading white space such as newlines, so all you get stored in your variable is "file.txt". The '\n' at the end is left for the next input function to deal with.
a=getchar();
OOPS - you lose.
getchar() just sees the '\n' left behind by the previous scanf() call and throws off your entire program logic.
/me *points to answer using fgets*
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
ok thanks a lot, i think this was getting a little beyond my skill level haha. but with the help of some guys at ipodlinux forums, it works! thanks a lot you guys,you helped a lot