473,320 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Why won't this program output my full name? - scanf() function, Xcode

Hello, Im pretty new to programing in C and need some help with some code that I wrote using Apple's Xcode C compiler. I am taking this example out of the C for dummies book, which is also posted below. Once the program runs and prompts me to enter my name, I enter "Martin Adams" (without the quotations of course). The output is always my first name though. I have tried altering the code a few different ways but it always throws an error message. What should I do to allow the program to output my full name?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     char me[20];
  6.  
  7.     printf("What is your name?\n");
  8.     scanf("%s" ,me);
  9.     printf("Darn glad to meet you, %s!\n" ,me);
  10.  
  11.     return(0);
  12. }
  13.  
Feb 8 '14 #1
4 1366
weaknessforcats
9,208 Expert Mod 8TB
scanf stops on whitespace. So it works great if your name is one word.

Look up the getline function. That function will get all characters until a \n is encountered.
Feb 8 '14 #2
mikele
6
Instead of scanf you can use also
Expand|Select|Wrap|Line Numbers
  1. gets(me);
May 6 '14 #3
donbock
2,426 Expert 2GB
gets is deprecated in the C99 C Standard; it has been removed from the 2011 C Standard; it is deprecated in the C++ Standard.
You should never ever use it.

I'm not familiar with getline; but you could use fgets.
May 6 '14 #4
weaknessforcats
9,208 Expert Mod 8TB
Sorry. getline is C++ not C.
May 6 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

54
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
2
by: DOA | last post by:
Help! I've been doing C language development for a long time, but this problem has me stumped. I would like the user to be able to enter the name of a function that has been linked into the...
2
by: Chris Clement | last post by:
I am trying to get the full name of a user not logged into a network on Windows 2000/XP. I know how to get the user name (cclement), but how do I get the full display name (Chris Clement) as it is...
10
by: Lance | last post by:
Is there any way to determine the full name of an object at runtime? For example, if I have the following Namespace MyNameSpac Public Class MyClas Protected MyObject As Object 'This could be...
4
by: Brian Henry | last post by:
I am creating a plugin system and each plug-in has a class called "Connect", which is what I created as the entry point for the plug-in. The problem is, I need to get the Full type name of this to...
4
by: Mindy | last post by:
hey, guys, I have a simple question --- simple only for you. I created a combo box called "pick up a name". And the underlying table has lastname and firstname columns. The full name can be...
10
by: madhura | last post by:
Hello, I have a problem with 2-d arrays and scanf function. I have wriitten a code in which i am accepting values in 1-d arrays int a,i; for(i=0;i<3;i++) scanf("%d",a+i); when I print the...
5
by: CindySue | last post by:
I'm not a programmer. I have a table called nicknames with two fields--a nickname field and a full name field. I also have a Licensed Provider table with a full name field and some other info. I have...
1
by: hbkiwi | last post by:
Hi, Just playing with Access fairly new to it (like days old). Also it's my first post here so if I'm in the wrong place, sorry.. What I'm wanting to know is basically I have lets call it Table...
12
by: sinbad | last post by:
hi, is it possible to get function name from function pointer. I am using gcc compiler on linux. thanks
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.