Avatar of huffjon
huffjon

asked on 

program 5

writing a program that accept a floating point number, an interger and a character.  then calls a function that puts all of the data in a single string.  here is what I ahve.

#include <stdio.h>

void getline(char []);

int main ()
{
      char stuff[90];
      int i;
      char numblah;
      char junk;

      printf("Enter a number");
      scanf ("%f", &i);
      printf("Enter a chatacter");
      scanf ("%f", &junk);
      printf("Enter a interger");
      scanf ("%f", &numblah);

      getline(stuff);

      printf("\nthe string you just entered is:\n");
      puts(stuff);

      return 0;
}

void getline(char string[])
{
      int i = 0;
      char c;

      while ( i < 89 && (c = getchar()) != '\n')
      {
            string [i] = c;
            ++i;

      }
      string [i] = '\0';
      return;
}
C

Avatar of undefined
Last Comment
ozo
Avatar of ozo
ozo
Flag of United States of America image

The scanf format for a character is %c and the scanf format for an integer is %d %i %u %o or %x depending on the radix and whether it is signed
sprintf(stuff,"%d%c%d\n",i,junk,numblah); would put it all into stuff
Avatar of huffjon
huffjon

ASKER

ok so why is it when I run my program that the only display is of the "interger line?
Avatar of ozo
ozo
Flag of United States of America image

For one thing, using an scanf parameter that does not match the format voids all guarantees about what your program might do.
but aside from that, the only thing that you display is stuff
and your getline will fill it with getchar()s which will read any characters left over that were not read by your scanf
Avatar of huffjon
huffjon

ASKER

ok so here is what I have now:

#include <stdio.h>

void getline(char []);

int main ()
{
      char stuff[90];
      int pound;
      float numblah;
      char junk;

      printf("Enter a number");
      scanf ("%d", &pound);
      
      printf("\nEnter a interger");
      scanf ("%f", &numblah);

      printf("\nEnter a chatacter");
      scanf ("%c", &junk);

      getline(stuff);

      printf("\nthe string you just entered is:\n");
      puts(stuff);

      return 0;
}

void getline(char string[])
{
      int i = 0;
      char c;

      while ( i < 89 && (c = getchar()) != '\n')
      {
            string [i] = c;
            ++i;

      }
      string [i] = '\0';
      return;
}

How do I make my string display the int, float and the char in a single string?
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
C
C

C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, so it has found lasting use in applications that had formerly been coded in assembly language, including operating systems as well as various application software for computers ranging from supercomputers to embedded systems. It is distinct from C++ (which has its roots in C) and C#, and many later languages have borrowed directly or indirectly from C.

23K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo