Thread: Why is my string duplicated in this very simple program?

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    19

    Unhappy Why is my string duplicated in this very simple program?

    Hi everyone, this is a small snippet of code from my school project:

    Code:
    #include <stdio.h>
    
    int main(void) {
      char menu[5][35] = {"Please select one of the following:", "A. Define Random Number List", "B. Sort Number List (High to Low)", "C. Sort Number List (Low to High)", "D. Exit"};
      for (size_t i = 0; puts(menu[i]), i < 4; i++);
      return 0;
    }
    When I run it, element 1 ("A. Define Random Number List") is printed twice. No matter what I try, element 1 is always duplicated.

    Why is this happening?

    EDIT: Fixed it, still not sure why that happened though. I wrote *menu[5] instead of menu[5][35] and that worked.
    Last edited by C-UL8R; 11-05-2020 at 02:18 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,337
    That sounds unlikely to me, and I cannot replicate the problem by compiling and running the code.

    Having said that, it would be better if you wrote the loop in a more conventional manner:
    Code:
    for (size_t i = 0; i < 5; i++)
    {
        puts(menu[i]);
    }
    Or even better:
    Code:
    for (size_t i = 0; i < sizeof(menu) / sizeof(menu[i]); i++)
    {
        puts(menu[i]);
    }
    EDIT:
    Oh, actually, I decided to do a count of your strings. "Please select one of the following:" is a string of length 35. It cannot fit into an array of 35 characters because you have to account for the null character. That explains your problem, and why I missed it: I didn't notice the trailing output on the first line.
    Last edited by laserlight; 11-05-2020 at 02:30 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
    Registered User
    Join Date
    Dec 2017
    Posts
    1,136
    I think laserlight didn't look closely enough at the output!

    You haven't given enough room for the strings. The first string is 36 chars long (including the '\0' at the end). Since you've only allowed for 35 chars it directly abuts the next string and both are printed out as a single string.
    The best argument against democracy is a five minute conversation with the average voter. - Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-24-2018, 11:38 PM
  2. Replies: 1
    Last Post: 01-18-2016, 06:12 PM
  3. Simple File and String Program Not Running
    By airfire29 in forum C Programming
    Replies: 4
    Last Post: 03-15-2015, 05:39 PM
  4. Replies: 9
    Last Post: 08-05-2011, 02:23 AM
  5. simple string program
    By twans in forum C Programming
    Replies: 3
    Last Post: 04-18-2004, 10:02 AM

Tags for this Thread