Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6986577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:49:37+00:00 2026-05-27T18:49:37+00:00

I am writing a simple console application to read 3 words and keep them

  • 0

I am writing a simple console application to read 3 words and keep them in an array. But after taking three inputs from the console it only displays the third word three times in the console. For example, if I give the input: “one”, “two” “three” the output shows only “three” 3 times).

Here is my code:

int main(int argc, char *argv[])
{
    char* input[30];  
    char word[30]; 

    int i=0;

    for(i=0; i<3 ;++i)
    {
       cin >> word;
       input[i] = word;
    }

    input[i] = 0;
    i=0;

    while(input[i])
    {
      cout << input[i] << endl;
      i++;
    }

    return 0;
}

What I am doing wrong here? Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:49:38+00:00Added an answer on May 27, 2026 at 6:49 pm

    You only have one buffer ( char word[30] ), which you are overwriting each time.

    When you do:

    input[i] = word;
    

    You’re assigning the address of the first element in that one and only buffer to each element in input (arrays degrade to pointers when you use the bare name). You end up with three copies of the same address in input[] (which is address of word, which contains the last thing your read from cin)

    One approach to fix this would be to use strdup() and assign the newly allocated string to your input[i]

    for(i=0; i<3 ;++i)
    {
       cin >> word;
       input[i] = strdup(word);
    }
    

    Also … if you only are going to have three input “words” you only need an array of 3 char pointers:

    char *input[3];
    

    and your output loop would look much like your input loop:

    for(i=0; i<3 ;++i)
    {
       cout << input[i] << endl;
    }
    

    Edit: Note that this answer was based on your wanting to use arrays. If this isn’t homework that requires that, see bobbymcr’s answer – when in C++, use C++.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a simple console application in Objective-C but I have heard that
I am writing a simple console application in LInux/C++ that accepts user input from
I'm writing a musical transposition application (in simple VB Console) and I want the
I am writing a simple java console game. I use the scanner to read
I'm writing a simple console application (80x24) in Java. Is there a gotoxy(x,y) equivalent
Okay, simple situation: I'm writing a simple console application which connects to a SOAP
I was writing a simple console application in Objective C. I have tried system(clear)
I'm writing an SDL application for Linux, that works from the console (no X
I'm writing a simple OpenGL application that uses GLUT . I don't want to
In a small console application I'm writing to test something I need to add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.