Thread: help in basic C

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    18

    help in basic C

    I need to create 2 programs and I am lost. first must take 3 inputs, a character, and integer, and a floating point; all from different prompts. Then I must call a function that will assemble all inputs into a single string. I must display using puts() call back when the main function has ended. This is what I have so far:

    insert
    Code:
    ;
    
    #include "stdafx.h"
    #include "stdio.h"
    #include "string.h"
    
    void strcat(int[], char[], float[]);
    #define max 10
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int n;
    	float f;
    	char c;
    	printf("Enter an integer:");
    	scanf_s("%d", &n);
    	printf("Enter a character:");
    	scanf_s("%c", &c);
    	printf("Enter a floating point number:");
    	scanf_s("%f", &f);
    	
    	
    strcat(n,"&c");
    	return 0;
    }
    Obviously I am far off. I also need some advice on how to complete this:

    1. 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.

    I've been taking this class online and I have had no feedback what so ever, so I have fallen really behind any and all help would be greatly appreciate.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Hint: sprintf(), http://www.cplusplus.com/reference/c...o/sprintf.html

    > void strcat(int[], char[], float[]);
    strcat() is a standard C function, and it doesn't look like that. http://www.cplusplus.com/reference/c...ng/strcat.html

    As for the linked list, there are countless tutorials on the web.

    Code:
    #include "stdafx.h"
    #include "stdio.h"
    #include "string.h"
    Should really be
    Code:
    #include <stdafx.h>
    #include <stdio.h>
    #include <string.h>
    BTW what is scanf_s? Do you mean scanf()? Or have you aliased scanf() to scanf_s() or something?

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    my compilier tells me to use scanf_s() to prevent errors, i don't know why.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    I do use strcat() to combine strings right? In my book i've only seen it used to combine two strings in the form of strcat(str1, "srting 2"); can I just add another string in quotations? and can i put an variable earlier defined or and adress(&n) in the function?

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    18

    what i've got so far on the linked list

    Here's what I've got so far on the linked list: It runs, but does not display the five inputs-instead i think it's displaying their addresses. Also I will need to adapt this program to prompt fot 1 more entry and to display the new list with the new entry.

    insert
    Code:
    #include <stdafx.h>
    #define maxnum 10
    struct value
    {
    	int num[maxnum]; 
    	struct value*nextaddr;
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int a,b,c,d,e,f;
    	printf("please enter five numbers:");
    		scanf("%f,%f,%f,%f,%f", &a,&b,&c,&d,&e,&f);
    		struct value t1={a};
    		struct value t2={b};
    		struct value t3={c};
    		struct value t4={d};
    		struct value t5={e};
    		struct value *first;
    		void display (struct value*);
    		first=&t1;
            t1.nextaddr=&t2;
    		t2.nextaddr=&t3;
    		t4.nextaddr=&t5;
    		t5.nextaddr=NULL;
    		display (first);
    
    	return 0;
    }
    void display(struct value*contents)
    {
    	while (contents!=NULL)
    	{
    		printf("%-5d", contents->num);
    		contents=contents->nextaddr;
    	}
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    scanf_s is a MS extended version of scanf. As described by MSDN:
    http://msdn2.microsoft.com/en-us/lib...et(VS.80).aspx

    This is an array.
    int num[maxnum];

    When you try to print it, it shows the address, because the array is converted to a pointer to the first element.

    You are also breaking the C standard rules by mixing code and variable declarations.
    Code:
    	printf("please enter five numbers:");
    		scanf("%f,%f,%f,%f,%f", &a,&b,&c,&d,&e,&f);
    		struct value t1={a};
    		struct value t2={b};
    		struct value t3={c};
    		struct value t4={d};
    		struct value t5={e};
    		struct value *first;
    		void display (struct value*);
    		first=&t1;
    This should be done by first declaring the struct value t1 .. t5, then setting the value to the relevant a .. f. [Or, better yet, write a function that adds a new element to a list using dynamic allocation, as that would easily allow you vary the number of elements in your list without changing the code at all]

    You are also giving the wrong format specifier for integers - %f is a floating point variant, whilst the a, ... f are integer values. You shoud use %d.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or turn off MS's attempt to redefine the world from their own perspective.
    http://cboard.cprogramming.com/showthread.php?t=78889
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    18

    sprintf function

    for my first problem it was advised to use the sprintf function. I believe it is to add variables together into a string as I need. But I'm not sure how to use it. this is what i got, but it doesn't work-it says;
    'sprintf' : cannot convert parameter 1 from 'void (__cdecl *)(int [],char [],float [])' to 'char *'

    insert
    Code:
    
    #include "stdafx.h"
    #include "stdio.h"
    #include "string.h"
    
    void str1(int[], char[], float[]);
    #define max 10
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int n;
    	float f;
    	char c;
    	printf("Enter an integer:");
    	scanf_s("%d", &n);
    	printf("Enter a character:");
    	scanf_s("%c", &c);
    	printf("Enter a floating point number:");
    	scanf_s("%f", &f);
    	
    	
    sprintf(str1, "%d,%c,%f", n,c,f);
    
    	return 0;
    }
    All I want to do is take an interger, floating point, and a character-assemble it into a string and display it using the puts function. It seems so easy but I can't get it.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > sprintf(str1, "&#37;d,%c,%f", n,c,f);
    You declared str1 as a function, not a char array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    18

    I've made some progress

    I've got the linked list to work, but I do not know how to then prompt for another entry, add it to the linked list, and then display it again. Can I just repeat the process? I don't think that would work, here's what it looks like.

    insert
    Code:
    #include "stdafx.h"
    #define maxnum 10
    struct value
    {
    	int num[maxnum]; 
    	struct value*nextaddr;
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int a,b,c,d,e,f;
    	printf("please enter five numbers:");
    		scanf("&#37;d,%d,%d,%d,%d", &a,&b,&c,&d,&e,&f);
    		struct value t1={a};
    		struct value t2={b};
    		struct value t3={c};
    		struct value t4={d};
    		struct value t5={e};
    		struct value *first;
    		void display (struct value*);
    		first=&t1;
            t1.nextaddr=&t2;
    		t2.nextaddr=&t3;
    		t3.nextaddr=&t4;
    		t4.nextaddr=&t5;
    		t5.nextaddr=NULL;
    		display (first);
    
    	return 0;
    }
    void display(struct value*contents)
    {
    	while (contents!=NULL)
    	{
    		printf("%-10d", *contents->num);
    		contents=contents->nextaddr;
    	}
    }
    On the first one I think i'm getting close, but thej prompts for the character and the floating point come up together, so i cannot decifer the inputs-and when it recieves the inputs it says the "stack around buffer was corrupted"

    insert
    Code:
    #include "stdafx.h"
    #include "stdio.h"
    #include "string.h"
    
    
    #define max 10
    int _tmain(int argc, _TCHAR* argv[])
    {
    	*str buffer[20];
    	int n;
    	char c;
    	float f;
    	printf("\nEnter an integer:\n");
    	scanf_s("%d", &n);
    	printf("\nEnter a character:\n");
    	scanf_s("%c", &c);
    	printf("\nEnter a floating point number:\n");
    	scanf_s("%f", &f);
    	
    	
    sprintf(buffer, "%d,%c,%f", n,c,f);
    puts(buffer);
    
    	return 0;
    }
    hopefully someone can help to get the kinks worked out
    Last edited by Salem; 10-07-2007 at 11:55 PM. Reason: Fix \code into /code at the end tag

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > *str buffer[20];
    What's this supposed to be? I'm surprised it even compiles.

    Try something like
    char buffer[100];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Well the first one builds and runs without errors. Though it does not stop to prompt for the character, accept it, and the repeat for the floating point-it asks for them at the same time and when it puts(), it only puts the interger correctly-i think the rest is the address. Thats for the advice so far. char buffer[100]; made it at least run without errors

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So do you still have a problem? If so, post your latest code and latest observations.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    well I after talking to someone about having it in a different format I tried:
    insert
    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
    	void combine(char*new_str1, int n, char c, float f);
    	char str1[100];
    	int n;
    	char c;
    	float f;
    
    printf("\nEnter an integer:\n");
    	scanf_s("%d", &n);
    	printf("\nEnter a character:\n");
    	scanf_s("%c", &c);
    	printf("\nEnter a floating point number:\n");
    	scanf_s("%f", &f);
    
    	combine(str1, n,c,f);
    	puts(str1);
    	return 0;
    }
    {
     combine(str1, n,c,f,)
     {	
    		str1=sprintf_s(str1, "%d, %f, %c", n,c,f);
    	return (str1);
     }
    }
    but it made me go backwards-it doesn't even run it says missing function header; which i thought is included. But the other way that almost works looks like this:
    insert
    Code:
    #include "stdafx.h"
    #include "stdio.h"
    #include "string.h"
    
    
    #define max 10
    int _tmain(int argc, _TCHAR* argv[])
    {
    	char str1[100];
    	int n;
    	char c;
    	float f;
    	printf("\nEnter an integer:\n");
    	scanf_s("%d", &n);
    	printf("\nEnter a character:\n");
    	scanf_s("%c", &c);
    	printf("\nEnter a floating point number:\n");
    	scanf_s("%f", &f);
    	
    	
    sprintf_s(str1, "%d,%c,%f", n,c,f);
    puts(str1);
    
    	return 0;
    }
    Also if you have time please look at my linked list: it works fine but I need to adjust it to prompt for another entry, add it to the linked list, and then re-display the new list. thanks for your help
    insert
    Code:
    #include "stdafx.h"
    #define maxnum 10
    struct value
    {
    	int num[maxnum]; 
    	struct value*nextaddr;
    };
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int a,b,c,d,e,f;
    	printf("please enter five numbers:");
    		scanf("%d,%d,%d,%d,%d", &a,&b,&c,&d,&e,&f);
    		struct value t1={a};
    		struct value t2={b};
    		struct value t3={c};
    		struct value t4={d};
    		struct value t5={e};
    		struct value *first;
    		void display (struct value*);
    		first=&t1;
            t1.nextaddr=&t2;
    		t2.nextaddr=&t3;
    		t3.nextaddr=&t4;
    		t4.nextaddr=&t5;
    		t5.nextaddr=NULL;
    		display (first);
    
    	return 0;
    }
    void display(struct value*contents)
    {
    	while (contents!=NULL)
    	{
    		printf("%-10d", *contents->num);
    		contents=contents->nextaddr;
    }

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    int _tmain(int argc, _TCHAR* argv[]) /*this is used for UNICODE/NON-unicode compilation
    because your code is not UNICODE complient - you may use the regular 
    int main() as well
    */{
    	void combine(char*new_str1, int n, char c, float f); /* this should be placed outside the function - as a common practice */	char str1[100];
    	int n;
    	char c;
    	float f;
    
    printf("\nEnter an integer:\n");
    	scanf_s("&#37;d", &n);
    	printf("\nEnter a character:\n");
    	scanf_s("%c", &c);
    	printf("\nEnter a floating point number:\n");
    	scanf_s("%f", &f);
    
    	combine(str1, n,c,f);
    	puts(str1);
    	return 0;
    }
    {/* and what is this braket doing here? */ 
     combine(str1, n,c,f,) /* wrong - should be like in the declaration
    void combine(char*new_str1, int n, char c, float f) */
     {	
    		str1=sprintf_s(str1, "%d, %f, %c", n,c,f); /* %f is for float, %c for char - your order is incorrect */
    	return (str1); /* void function cannot return values */
     }
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  2. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  3. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM
  4. Basic Graphics programming in C, please help
    By AdRock in forum Game Programming
    Replies: 3
    Last Post: 03-24-2004, 11:38 PM