Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So, the printf is not working.
C++
#include "stdio.h"
int main(void) {
  int aa;
  printf("Hello. This code will write a story, based on your choice.\n");
  printf("What is the name of your character?");
  scanf("%i",&aa);
printf("Your name is %i",&aa);

  return 0;
}


What I have tried:

The printf is not working I need help
Posted
Updated 20-Jan-21 8:01am
v2
Comments
CPallini 20-Jan-21 13:06pm    
Reading the documentation would help.

C++
printf("Your name is %i",&aa);

The variable aa is an int type so does not require the addressof operator (&) on a printf statement. However, it is unlikely that you actually want an integer as your character's name.
 
Share this answer
 
Comments
Nelek 20-Jan-21 11:06am    
in other words... the printf is working, the only thing is... it is not working as the OP expects :)
Richard MacCutchan 20-Jan-21 11:17am    
As is so often the case. :)
To add to what Richard has said, the printf format code for printing a null-terminated string (like a persons name) is "%s" not "%i"
You probably want to read a name into a string of some form, so declare aa as an array of characters:
C++
char aa[100];
and use the correct format code on both your scanf and printf lines.
Or better, change the name of the variable to reflect what it contains, and your code becomes more readable:

C++
#include <stdio.h>

int main(void) 
   {
   char characterName[100];
   printf("Hello. This code will write a story, based on your choice.\n");
   printf("What is the name of your character? ");
   scanf("%s",characterName);
   printf("Your name is %s",characterName);
   return 0;
   }
Note that you don't have to get the address of the variable to pass it to eit5her scanf or printf - the name of an array is defined to be a pointer to it's first element.

Also note that scanf will stop reading at the first whitespace character - so if your user wants to be called "Hagar the Horrible" he will end up as "Hagar" alone. To get the whole name, you probably want to call fgets[^] and pass it stdin as the stream.
 
Share this answer
 
You should turn up the warnings on your compiler - It would flag your issue right away. The issue is here
C
printf("your name is %i", &aa);

The format specifier "%i" expects an integer, but &aa is pointer to integer, so you're probably getting something like
Your name is --171568772
which would be the value of the address of variable aa expressed as an integer.

It's usual to use < and > to indicate system headers, such as stdio.h. This is not strictly an error, since the program still compiles, but it does add to the compile time, as includes enclosed int qoutes are searched for starting in the local directory. Those enclosed in <> are only searched for in the compiler include locations.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900