I need to add prompts for the number, integer and character inputs separatly instead of all at once. I want it to say "enter a number" wait for input, "enter an integer" wait for input and "enter a character" wait for input. Then it is suppose to combine the inputs and print the out using the puts() command. Here is what I got, the program does what I want except for the prompts.

#include <stdio.h>
void getline(char []);
#define LSIZE 4

int main()
{
 
    char message[LSIZE];
    
    printf("Enter a number, integer and character \n");
    getline(message);
    printf("The string just entered is:\n");
    puts(message);
    
   
    return 0; 
}

void getline(char strng[])
{
     int i=0;
     char c;
     
     while(i < (LSIZE-1) && (c = getchar()) != '\n')
     {
           strng[i] = c;
           i++;
             
     }
     strng[i] = '\0';
}

Recommended Answers

All 9 Replies

There's no reason you can't separate them.

int i;
double d;
char c;
printf( "Please enter a number\n" );
getline( message );
d = atof( message );

printf( "Please enter an integer\n" );
getline( message );
i = atoi( message );

printf( "Please enter a character\n" );
getline( message );
c = message[ 0 ];

what does the atof and atoi mean? I tried to run it with what you ahd written and it came up with undeclared, first use for atoi and atof.

what does the atof and atoi mean? I tried to run it with what you ahd written and it came up with undeclared, first use for atoi and atof.

atoi --> ascii to integer
atof --> ascii to float

here is the updated code, but I get still get the indeclard first use when I compile on the atoi and atof.

#include <stdio.h>
void getline(char []);
#define LSIZE 4

int main()
{
    char message[LSIZE];
    
    int i;
    double d;
    char c;
    printf( "Please enter a number\n" );
    getline( message );
    d = atof( message );

    printf( "Please enter an integer\n" );
    getline( message );
    i = atoi( message );

    printf( "Please enter a character\n" );
    getline( message );
    c = message[ 0 ];

    printf("The string just entered is:\n");
    puts(message);

    return 0; 
}

void getline(char strng[])
{
     int i=0;
     char c;
     
     while(i < (LSIZE-1) && (c = getchar()) != '\n')
     {
           strng[i] = c;
           i++;
             
     }
     strng[i] = '\0';
}

SO I had to add the <stdlib.h> to not get the undeclared, but now I get the right input prompt, but the output for the final message is the last character that I entered. Any thoughts?

SO I had to add the <stdlib.h> to not get the undeclared, but now I get the right input prompt, but the output for the final message is the last character that I entered. Any thoughts?

You keep reclycling message every time you ask for a value. After last call to getline, after collecting the char value, you want puts to show what is in char message; what do you thing is going to be there?
If you printf whatever is in i, d, and c you'll find the value you're looking for. Only if the conversion was successful

Is there a way to start the message and then add on the message with strcat?

>Is there a way to start the message and then add on the message with strcat?
I am not sure what you are asking here, however based in you first post requirements. You don't need the use of atoi nor atof. You are collecting every input as a string using the getline() function. And you intend to display it as a string using the function puts()

char message[BIG_FOR_ALL]; /* long enough to hold the concatenation of every question  plus '\0'*/
 char temp[ENOUGH]; /* enough to hold any string representing a double floating point or 20 characters in a 32 bit system  plus a '\0'  */
 .
 .
 .

 getline( temp );
 strcat( message, temp );
 .
 . /* every time you ask for input */
 .
 puts( message );

Yes, yes, of course.

I just noticed that LSIZE is 4. That means your string can't have more than 3 printable characters in it, plus the null at the end. It is waaay too small.

#include <stdlib.h>
#include <stdio.h>

#define LSIZE 200

int main() {
  char inputstr[ LSIZE ];
  char message[ LSIZE ];
  double d;
  int i;
  char c;

  printf( "Please enter a number" );
  getline( inputstr );
  d = atof( inputstr );
  strcpy( message, inputstr );

  getline( inputstr );
  ...
  strcat( message, ", " );
  strcat( message, inputstr );
  ...
  ...
  puts( "The THREE strings you just entered are:" );
  puts( message );
  ...
  puts( "Again, the THREE things you just entered are:" );
  printf( "%g, %d, %c\n", d, i, c );

  return 0;
  }

You'll notice that this code does things two different ways. Choose one.

[EDIT] man, too slow again... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.