By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

Some Help Please with Strings and Structures

P: 10
I've been tasked to do 2 questions. I think i got the majority of it done, but i'm having trouble.
Question #1:
Write a C program that accepts as input from the keyboard a floating point number, an integer, and a character. Each of these inputs should be preceded by a prompt and stored using individual variable names. Have your program call a function that assembles the input data into a single string. Display the assembled string using the 'puts()' call back in 'main' after the function has completed.
Here's what i have so far:
#include <stdio.h>
#include <string.h>

int main()
{
#define MSIZE 50
char message[MSIZE];
float number1;
int number2;
char char1;

printf("Please enter a character: ");
scanf("%c", &char1);

printf("Please enter a floating point number: ");
scanf("%f", &number1);

printf("Please enter an integer: ");
scanf("%d", &number2);

gets(message);


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


system("pause");
return 0;
}

I'm trying to get the inputted data together by some how using the 'strcat' function that's why included <string.h>. that way i can "make" a message out of it...

Here's the 2nd Question:
Create a structure that has one variable called 'value' and one pointer to the list (making it a linked list). Prompt for 5 values from the keyboard as input and store them in the linked list. Print out the current contents of the list. Allow the user to add one more 'value' to the linked list, and print the contents of the list again.
Here's what i have so far:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i;
int *value;

value = (int *) malloc(5 * sizeof(int));

for(i = 0; i < 5; i++)
{
printf("Enter a value: ");
scanf("%d", &value[i]);
}

printf("Here are the values you entered into the array so far: \n");

for (i = 0; i < 5; i++)
printf("%d\n", value[i]);

printf("Please enter one more value: ");
scanf("%d");


system("pause");
return 0;
}

i've got the majority of it correct (i think) it's the adding of 1 more value that screws me up. can somebody help me with my questions. thanks
All insights or suggestions are greatly appreciated!.
Thanks for your time in advance :)

-t
Jun 3 '07 #1
Share this Question
Share on Google+
2 Replies


kky2k
P: 34
I have some questions to ask.In ur first coding example
U r displaying the string message.Fine but where r u copying ur i/p's to that string?

so what does it mean gets(message);?

let me give u one hint to solve that problem..how abt sprintf();

also try to #define outside a fn.

Abt ur second coding...
where is the structure for ur linked list?
U r simply coying the datas into a array not into a linked list..
so in order to create a linked list in C,u gotto know abt structure in C...


Hope this will help u.....
Jun 3 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
First things first. You have a char, int and float. These need to be converted to a string.

So, you have to convert each of these things to a string and then append the strings together to make the message. May be somthing like:

Expand|Select|Wrap|Line Numbers
  1. void MakeMessage(char* the messagearray, float n1, int n2, char n3);
  2.  
Remember, the puts() function requires a string so be sure yours has the \0 terminator.

Call this function in main():

Expand|Select|Wrap|Line Numbers
  1. MakeMessage(message, number1, number2, char1);
  2.  
Doing it this way localizes all the conversion code to the MakeMessage() function.

Get this part working first. Then sunmit a new post for the linked list issue.
Jun 3 '07 #3

Post your reply

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