newbie problem

This is a discussion on newbie problem within the C Programming forums, part of the General Programming Boards category; Hello, I am just starting out in learning C and I want to make a simple program that creates a ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    17

    newbie problem

    Hello,

    I am just starting out in learning C and I want to make a simple program that creates a string, from the options a user inputs and then sorts that string into a correct format for display.
    I have put a comment at where the program is going wrong, can anyone tell me where I am going wrong?

    Also I was wondering, I want to put this into a gui, would I be best coding the gui into it at the same time, or is it just as easy to get it working correctly in command prompt, then
    putting the gui in around it?


    Code:
    if (num == 2)  {
                   strcat(string, define1);
                   printf(" \n\n a - define2\n b - define3\n c - define4\n d - define5s\n e - define6\n f - define7\n");
                   b = getchar();
                   }
                        if (b == 'a') {
                        strcat(string, define2);
                        }
                             else if (b == 'b') {
                             strcat(string, define3);
                             }
                                  else if (b == 'c')  {
                                  strcat(string, define4);
                                  }
                                       else if (b == 'd') {
                                       strcat(string, define5);
                                       }
                                            else if (b == 'e')  {
                                            strcat(string, define6);
                                            }
                                                 else if (b == 'f') {
                                                 strcat(string, define7);
                                                 }
                             if (b == 'e' || b == 'f') {
                             printf(" \n\n a - define12\n b - define13\n c - define14\n d - define15\n");
                             c = getchar();    // works good up to here, it prints above statement, but then ends rather than asking for the next option. 
                             }                                          
                                  if (c == 'a')  {
                                  strcat(string, define12);
                                  }
                                       else if (c == 'b') {
                                       strcat(string, define13);
                                       }
                                            else if (c == 'c')  {
                                            strcat(string, define14);
                                            }
                                                 else if (c == 'd') {
                                                 strcat(string, define15);
                                                 }
    Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by redsfan View Post
    can anyone tell me where I am going wrong?
    Yep. This is the number one most common problem for newbies at cboard which is why I wrote this:

    STDIN pitfalls

    Basically, this happens because you need to push return, which "\n" is also a character and it gets left in the stdin buffer. Try this:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
    	int x = getchar(), y = getchar();
    
    	printf("ASCII: %d %d\n",x, y);
    
    	return 0;
    }
    The second one will always be ascii 10, the newline. (read that page!)

    Also I was wondering, I want to put this into a gui, would I be best coding the gui into it at the same time, or is it just as easy to get it working correctly in command prompt, then
    putting the gui in around it?
    What library are you going to use for the GUI? I'm presuming you are not sure and have not done any GUI programming yet. I'm also going to recommend you forget about it for a while. GUI stuff is much more complicated that you think it is going to be. You need to develop some basic C skills first.
    Last edited by MK27; 01-26-2010 at 09:58 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    17
    Thanks mk27 that explains it and it was fixed by putting in fflush(stdin);

    Could you tell me what you are doing in this following line?

    Code:
    int main(int argc, char *argv[])
    It looks like your creating an integer variable argc and a character variable *argv but why put these inside the main() function? and what is the purpose of the astrix?

    What library are you going to use for the GUI? I'm presuming you are not sure and have not done any GUI programming yet. I'm also going to recommend you forget about it for a while. GUI stuff is much more complicated that you think it is going to be. You need to develop some basic C skills first.
    Well I use MinGW and I have seen glade mentioned as a good gui builder, but I don't really have any idea and have never done any gui programming, I will leave it alone for now though, I no I have much to learn yet.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by redsfan
    that explains it and it was fixed by putting in fflush(stdin);
    Unfortunately, that is not a correct fix as fflush() is undefined for input streams like stdin. Rather, you should do something like use getchar() to discard what remains on the input stream.

    Quote Originally Posted by redsfan
    Could you tell me what you are doing in this following line?
    Read the tutorial on command line arguments.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    17
    Quote Originally Posted by laserlight View Post
    Unfortunately, that is not a correct fix as fflush() is undefined for input streams like stdin. Rather, you should do something like use getchar() to discard what remains on the input stream.
    Ahh I see, that works fine, thanks.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by redsfan View Post
    Ahh I see, that works fine, thanks.
    Of course, redfan could also have read the page, where I explain why not to use fflush(stdin) and provide some alternatives... Which IMO another getchar() is the simplest, but not the best, and you should understand the why of this.

    ps. the command line arguments just got left into that example because I tend to always use the same "template file" when testing and evidently last time that involved command line args. Normally "int main()" or "int main (void)" are fine.
    Last edited by MK27; 01-26-2010 at 12:38 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    17
    Quote Originally Posted by MK27 View Post
    Of course, redfan could also have read the page, where I explain why not to use fflush(stdin) and provide some alternatives...
    Sorry I didn't know there was a link in there, I was wondering what page you were reffering too So I went of looking when you told me what the problem was and fflush(stdin)
    was the first alternative I came across. I will read through the page you provided, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  2. Newbie problem with scanf function...
    By Mithal in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 09:28 PM
  3. Newbie ... looping problem
    By StupidIntel in forum C++ Programming
    Replies: 12
    Last Post: 05-13-2004, 06:45 PM
  4. Problem with code (newbie question)
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2002, 01:39 AM
  5. newbie coding problem
    By rippascal in forum C++ Programming
    Replies: 10
    Last Post: 01-08-2002, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21