Thread: How to rewrite this code so I don't need str1 and str2 (i.e. no conversion)

  1. #1
    zach
    Guest

    How to rewrite this code so I don't need str1 and str2 (i.e. no conversion)

    Code:
    #include<conio.h>
    #include<stdio.h>
    #include <string.h>
    
    char str1[10];
    char str2[10];
    
    void writeDes(char str1[],char str2[]);
    
    int main(void)
    {    
        char name[10];
        char code[10];
        
        char *p;
        
        fgets(name, sizeof(name), stdin );
        if ( (p=strchr(name,'\n')) != NULL ) *p = '\0';
        
        char *str1 = name;
    
        fgets(code, sizeof(code), stdin );
        if ( (p=strchr(code,'\n')) != NULL ) *p = '\0';
        
           char *str2 = code;
           
           writeDes(str1,str2);
           
        return 0;
    }
    
    void writeDes(char str1[],char str2[])
    {
        printf(str1);
        printf("\n");
        printf(str2);    
    }
    I am new to C and am finding passing parameters to functions a bit difficult, so this is a test program. It does work but I would like to do away with having to convert name and code to str1 and str2. I want only to use name and code. Thank you, zach.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Haven't you tried writing writeDes(name, code)?

    By the way, your printf calls that pass a string -- especially from user input -- as an argument via a variable should include a format string, even if the format string is only "%s". Otherwise, your string argument could result in undefined behaviour if it contains format specifications, whether coincidentally or by malicious intent.
    Last edited by laserlight; 08-15-2019 at 04:15 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    zach
    Guest
    Quote Originally Posted by laserlight View Post
    Haven't you tried writing writeDes(name, code)?

    By the way, your printf calls that pass a string -- especially from user input -- as an argument via a variable should include a format string, even if the format string is only "%s". Otherwise, your string argument could result in undefined behaviour if it contains format specifications, whether coincidentally or by malicious intent.
    To answer your question: Yes I did and I couldn't get it to work.

  4. #4
    Registered User
    Join Date
    May 2019
    Posts
    214
    void writeDes(char * str1,char * str2);

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zach
    Yes I did and I couldn't get it to work.
    How did it not work? What was the exact code that you tried and what was the error message?

    Quote Originally Posted by Niccolo
    void writeDes(char * str1,char * str2);
    Except for spelling, that's equivalent to zach's current writeDes function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    zach
    Guest
    Quote Originally Posted by laserlight View Post
    How did it not work? What was the exact code that you tried and what was the error message?


    Except for spelling, that's equivalent to zach's current writeDes function.
    Sorry, I do not remember the different alternatives that I tried, and didn't work.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Quote Originally Posted by zach View Post
    Sorry, I do not remember the different alternatives that I tried, and didn't work.
    Maybe you should change your username to RyanD.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could have just tried again and showed the result. Since you have no proof, I conclude that you are mistaken, so my advice is to write: writeDes(name, code)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Ouch.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Something like this zach.
    Code:
    //!! obsolete, don't use #include<conio.h>
    #include<stdio.h>
    #include <string.h>
    
    // pointless globals
    // char str1[10];
    // char str2[10];
    
    void writeDes(char str1[],char str2[]);
    
    int main(void)
    {
        char name[10];
        char code[10];
    
        char *p;
    
        fgets(name, sizeof(name), stdin );
        if ( (p=strchr(name,'\n')) != NULL ) *p = '\0';
    
    //    char *str1 = name;
    
        fgets(code, sizeof(code), stdin );
        if ( (p=strchr(code,'\n')) != NULL ) *p = '\0';
    
    //       char *str2 = code;
    
           writeDes(name,code);
    
        return 0;
    }
    
    void writeDes(char str1[],char str2[])
    {
      //!! NEVER pass user input as the format string
      //to printf.
        printf("S1=%s",str1);
        printf("\n");
        printf("S2=%s",str2);
        printf("\n");
    }
    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.

  11. #11
    zach
    Guest
    Thank you Salem! Zach.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's pretty much what I suggested that you do, except that I was willing to work through the errors that you initially faced so you can learn from it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    zach
    Guest
    Hi, I am very grateful for your help! Zach.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. if(!strcmp(str1, str2)
    By Ducol in forum C Programming
    Replies: 5
    Last Post: 03-13-2016, 10:43 AM
  2. Gray Code to Binary Code Conversion!
    By JKrakauer in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2011, 03:49 AM
  3. Difference between char *str1 & const char *str2
    By Tigers! in forum C Programming
    Replies: 4
    Last Post: 08-06-2009, 04:04 AM
  4. Rewrite code in structure way
    By ninety3gd in forum C Programming
    Replies: 3
    Last Post: 06-11-2009, 08:49 PM
  5. need function like strncmp(str1, str2, n)
    By evader in forum C Programming
    Replies: 5
    Last Post: 09-23-2001, 08:42 PM

Tags for this Thread