string help needed

This is a discussion on string help needed within the C Programming forums, part of the General Programming Boards category; I am a newbie and just learning about strings. I am trying to create a program that accepts a floating ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    string help needed

    I am a newbie and just learning about strings. I am trying to create a program that accepts a floating point, an integer,and a character.
    The program must have a call function that assembles the input data into a string. It must display the assembled string using puts() call back after the main function has completed. Here's what I have, but Have the following errors and cannot figure it out. Any help is greatly appreciated.
    Code:
     
    
    #include <stdio.h>
    void combine (char *, int, float*);
    int main()
    {
    char strng1[80],strng2[100];
    int num, char*,float*;
    printf("enter a string:");
    gets(strng1);
    printf("enter an integer number:");
    scanf("%d", &num);
    printf("enter a character:");
    scanf("%c", &char*);
    printf("enter a floating point:");
    scanf("%f",&float*);
    combine (strng1, strng2, int, float*,char*);
    printf("A string containing all inputs is:");
    puts(strng2);
    return 0;
    }
    void combine(char*source,char *dest, int,float*)
    {
    sprintf(dest,"%c%d%1f",source, float*);
    return;
    }

  2. #2
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Code:
    void combine (char *, int, float*);
    combine (strng1, strng2, int, float*,char*);
    void combine(char*source,char *dest, int,float*)
    wow.No two are same.


    Code:
    int num, char*,float*;
    what this thing is doing.
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    c help strings

    I am still geeting the following errors even when I change the int num, char* and Float*


    Compiling...
    string problem.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\prob2-float\string problem.cpp(6) : warning C4518: 'char ' : storage-class or type specifier(s) unexpected here; ignored
    C:\Program Files\Microsoft Visual Studio\MyProjects\prob2-float\string problem.cpp(6) : error C2059: syntax error : ','
    C:\Program Files\Microsoft Visual Studio\MyProjects\prob2-float\string problem.cpp(12) : error C2062: type 'char' unexpected
    C:\Program Files\Microsoft Visual Studio\MyProjects\prob2-float\string problem.cpp(14) : error C2062: type 'float' unexpected
    C:\Program Files\Microsoft Visual Studio\MyProjects\prob2-float\string problem.cpp(15) : error C2062: type 'int' unexpected
    C:\Program Files\Microsoft Visual Studio\MyProjects\prob2-float\string problem.cpp(21) : error C2447: missing function header (old-style formal list?)
    Error executing cl.exe.

    string problem.obj - 5 error(s), 1 warning(s)

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    try puttng those on three different lines, and giving the variables a name, such as below. The comma operator, as you used in your original program, only works if all three variables are the same type. You tried to declare three variables each of different types, so a semicolon is required between each one.
    Code:
    int num;
    char* charArray;
    float* floatArray;
    and make sure the parameters are the same on that combine function. Your compiler will scream at you if they are not consistent with each other.
    Last edited by Ancient Dragon; 11-10-2005 at 10:43 AM.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    Quote Originally Posted by tmitch
    I am still geeting the following errors even when I change the int num, char* and Float*
    What have you exactly changed?? It seems to me you are starting to learn c language as well. char and float are reserved keywords of the language.
    Try something like
    Code:
    #include <stdio.h>
    #include <string.h> /* string utilities */
    
    int
    main()
    {
    int num;
    char c;
    float floatNum;
     
    /* YOUR CODE HERE but make sure you use the correct variables */
    
    return 0;
    }
    Have a look on sprintf() as well.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    made some corrections to the variable names-still errors

    still getting 3 errors with parameters ( combine) and function header and & errors!! please help


    Code:
    #include <stdio.h>
    #include <string.h>
    void combine (char c, int num, floatnum);
    int main()
    {
    char strng1[80],strng2[100];
    int num;
    char c;
    floatnum;
    printf("enter a string:");
    gets(strng1);
    printf("enter an integer number:");
    scanf("%d", &num);
    printf("enter a character:");
    scanf("%c", &c);
    printf("enter a floating point:");
    scanf("%f",&floatnum);
    combine (strng1, strng2, &num, floatnum, &c );
    printf("A string containing all inputs is:");
    puts(strng2);
    return 0;
    }
    void combine(char c,char *dest, int num,floatnum)
    {
    sprintf(dest,"%c%d%1f",source, floatnum);
    return 0;
    }







    Quote Originally Posted by odysseus.lost
    What have you exactly changed?? It seems to me you are starting to learn c language as well. char and float are reserved keywords of the language.
    Try something like
    Code:
    #include <stdio.h>
    #include <string.h> /* string utilities */
    
    int
    main()
    {
    int num;
    char c;
    float floatNum;
     
    /* YOUR CODE HERE but make sure you use the correct variables */
    
    return 0;
    }
    Have a look on sprintf() as well.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tmitch
    still getting 3 errors with parameters ( combine) and function header and & errors!! please help
    Try paying attention to what you write:

    Code:
    void combine (char c, int num, floatnum);
    First, you have this.
    Code:
    char strng1[80],strng2[100];
    int num;
    char c;
    floatnum;
    We'll just ignore the whole gets thing right now, but go read the FAQ on it anyway...
    Code:
    combine (strng1, strng2, &num, floatnum, &c );
    You call that, which is FIVE arguments. Look up above again.
    Code:
    void combine(char c,char *dest, int num,floatnum)
    Finally, you have that, which is FOUR arguments. You sure you can't figure out what's wrong yet?

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    No, I guess it has something to do with the combine statement and the different variables, but I am not sure


    Quote Originally Posted by quzah
    Try paying attention to what you write:

    Code:
    void combine (char c, int num, floatnum);
    First, you have this.
    Code:
    char strng1[80],strng2[100];
    int num;
    char c;
    floatnum;
    We'll just ignore the whole gets thing right now, but go read the FAQ on it anyway...
    Code:
    combine (strng1, strng2, &num, floatnum, &c );
    You call that, which is FIVE arguments. Look up above again.
    Code:
    void combine(char c,char *dest, int num,floatnum)
    Finally, you have that, which is FOUR arguments. You sure you can't figure out what's wrong yet?

    Quzah.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    I know it has something to do with the combine variables, but I am not sure what is rt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  2. Replies: 1
    Last Post: 05-30-2003, 02:31 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21