Skipping part of the code.

This is a discussion on Skipping part of the code. within the C Programming forums, part of the General Programming Boards category; Hey, I wrote a simple prog. that is asking 2 questions and then a question with Y/N answer.. but when ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    65

    Skipping part of the code.

    Hey, I wrote a simple prog. that is asking 2 questions and then a question with Y/N answer.. but when the first 2 questions are asked, the program skip the Y/N question and jump down to return 0; and quit.
    (And yes, I use cin instead of scanf, because scanf is.. just stupid)

    Please help :P

    Code:
    int main()
    {
    	char Fname[30];
    	char Ename[30];
    	char c;
    
    printf("Please enter the first name of the admin\n: ");
    	cin >> Fname;
    	printf("Please enter the surname\n: ");
    	cin >> Ename;
    	printf("Is the name %s correct? (Y/N)\n: ", Fname,Ename);
    	c=getchar();
    		if(c=='Y' || c=='y')
    		{
    			printf("Welcome Mr.%s", Ename);	
    		}
    		else
    		{
    			printf("Okey then.");
    		}
    	
    	return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Is this C or C++? You should pick one... scanf is not stupid (not completely) but you shouldn't mix C/C++ I/O.

    Code:
    printf("Is the name %s correct? (Y/N)\n: ", Fname,Ename);
    Only one format specifier in the printf, but two arguments?

    When you say "skipping" you mean that the program window just closes too fast for you to see any output?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    But cin is C++ and scanf is C... Prehaps use cin.ignore(); after cin... read Daved's response here Beginner problem (#3)

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    C code, I used scanf to begin with, but when all of my codes came up with the same problem, bufferts (if that how you spell it in english)
    cin just did everything, a bit easier.. I know that I´m not supposed to take the easy route but I did it this time.

    about the two arguments, I saw that myself when I posted it, corrected it in the compiler, but that does not change the problem.
    The thing is, after I enter the surname, the program quits, it´s not even showing the Y/N question, it just, skips it totaly.
    tried to put in some getch(); randomly to see where the problem was, but if I put it in the end the program decides by itself and picks the True statement.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    It's cin.ignore()... I think. Kinda makes sense. You should decide to either use C or C++, as hk said. A mixture will only help to confuse everyone.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    Then some1 can tell me the + with using c++ .
    because I like printf() for some reason but I hate scanf() and so on

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Lets put it this way... You can use printf in C++, but you can't use cin in C.

    Edit: Note that's not to say use C++ because of it.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    Well cin.ignore(); worked just fine!

    thx for the fast replies, and maybe I should read up some more on scanf();
    just in case, maybe I start to like it ^^

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    You could always use fgets()?

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    If I knew what it was then sure maybe! I have to read more, cin.ignore will have to work for now atleast ^^ thx anyhow mmcg

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    You can use cout instead of printf in C++...
    But actually... what is your purpose for learning? What is your goal?
    It can help in deciding what language you should learn.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    Here you go:

    Quote Originally Posted by Dan Gookin
    2.1.3 What can I use instead of gets()?

    Until I publish my booklet on console programming. I suggest using the fgets() function as a temporary solution and replacement for gets(). Here is the format:

    fgets(buffer,size,stdin);

    buffer is the location of your string storage space or buffer.

    size is the number of characters to input. This sets a limit on input and therefore it's not "dangerous" as gets() is.

    stdin is the standard input devices, the filename for the keyboard.

    So:
    Code:
    #include <stdio.h>
    
    int main()
    {
       char buffer[128];
    
       printf("Type something: ");
       fgets(buffer,127,stdin);
       printf("You typed &#37;s\n",buffer);
       return(0);
    }
    Note that fgets(), unlike gets() also reads in the carriage return (Enter key) at the end of the line. That character becomes part of the string you input. That may screw things up a bit, but it's a simple workaround and avoids the Linux "gets() is dangerous" warning.
    It's a better function than scanf() and gets(), and it'll keep your code as C and not a mixture of C/C++!

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    The example does get fgets wrong, though.
    The code should be:

    Code:
    #include <stdio.h>
    
    int main()
    {
       char buffer[128];
    
       printf("Type something: ");
       fgets(buffer, sizeof(buffer), stdin);
       printf("You typed %s\n", buffer);
       return 0;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    65
    Elysia, it it any differences between c and c++ in the ability to make things? like programs and games and so on?

    My friend who have been programming for the last 4 years is still mixing c and c++, atleast that is what he said, he is using cin and printf.
    And he is now working as a programmer for a company.

    He sure does have a deeper in-sight in programming than I do (Maybe the 4 years diff. ^^)
    but he seem to manage great.
    But what do I know, still a noob in learning, silly padwan.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by And3rs View Post
    Elysia, it it any differences between c and c++ in the ability to make things? like programs and games and so on?
    In terms of language, it's huge.
    C++ has a great number of features that makes programming easier. It's built for processing modern applications.
    C is a complex, low level language that is not as flexible as C++. It is hard to program in, and doesn't really have any advantage over C++, except that it's still heavily used in embedded systems.
    For example, C++ has a lot of stuff such as dynamic arrays, smart pointers (no need to manually call new/delete as malloc/free), being able to create generic code (code that can be re-used for many things), such as templates (the ability to write functions that processes different data types), etc. C does not have these.

    My friend who have been programming for the last 4 years is still mixing c and c++, atleast that is what he said, he is using cin and printf.
    And he is now working as a programmer for a company.
    Well, I can only say your friend is missing out on a lot of C++ feature if he keeps mixing C and C++. It's not really "good" either.
    Last edited by Elysia; 10-01-2008 at 12:06 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code writing issues Part 2...
    By jaybo684 in forum C++ Programming
    Replies: 10
    Last Post: 08-01-2005, 08:28 AM
  2. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM
  4. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

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