Like Tree1Likes
  • 1 Post By Elysia

C++:Simple program using 2d arrays?

This is a discussion on C++:Simple program using 2d arrays? within the C++ Programming forums, part of the General Programming Boards category; I'm writing a simple program that puts a string in some order like: The string is "Once upon a time" ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    9

    C++:Simple program using 2d arrays?

    I'm writing a simple program that puts a string in some order like:
    The string is "Once upon a time"
    My program has to put each word in a new line when it reads a space:
    Once
    upon
    a
    time
    But, i can only get 'On' and nothing else
    Here:
    Code:
    int whichWord = 0;
    int wordIndex = 0;
    int sentenceIndex = 0;
    char sentence[MAX_STR_LEN];
    char WORDS[MAX_WORDS][MAX_STR_LEN];
    char word[] = "Once upon a time ";
    int counter;
    
    
    
    
    strcpy(sentence, word);
    cout << sentence << endl; 
    
    
    while( sentence[sentenceIndex] != NULL_CHAR )
    { 
    if (sentence[sentenceIndex] != SPACE )
    {
    WORDS[whichWord][wordIndex] = sentence[sentenceIndex];
    wordIndex++;
    }
    else
    {
    WORDS[whichWord][wordIndex] = NULL_CHAR;
    wordIndex++;
    wordIndex = 0;
    }
    sentenceIndex++;
    }
    
    WORDS[whichWord][wordIndex] = NULL_CHAR;
    
    for( counter = 0; counter <= whichWord; counter++)
    {
    cout << WORDS[counter] << endl;
    }
    



  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    You could begin with simplifying and fixing and indenting the code.
    Simplify and fix by getting rid of char in favor of std::string. Then simplify by using the find function instead of pointless looping. Cleaner, better code.
    Then indent all your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    First you must in my opinion read about Indent_style .

    Then it would be far better for everybody that wants to help you ( and not only ) to wrap your code in code tags.This will happen if you write like this

    [key] /* YOUR CODE HERE*/ [/key] Replace key with code in order this to work.

    If understood well , your goal is to read a phrase.Then you will assign every word in an element of your array.The first string goes to the first element and so on... You know how to locate strings ( by the space ) .
    I also suggest you to use the string class .

    I could not read your code :/ (mostly because of indentation)

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    Wow! Your answers were excalty what I needed. So helpful. Instead of pointing out my mistakes, how about maybe give a good answer.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,197
    Do you think an attitude like that will make people want to help you?
    You've already been given two suggestions for your program, in addition to the deserved lectures on making your code readable so it's easier for others to help you. Have you looked into the program-related suggestions? If so, were they applicable and helpful? Why or why not?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by rvbplayer123 View Post
    Wow! Your answers were excalty what I needed. So helpful. Instead of pointing out my mistakes, how about maybe give a good answer.
    Do you think I want to read your messy, unclean and unreadable code? I don't.
    Does that mean that others will? The chance of that decreases dramatically the worse the quality of your code is.
    So what must you do then? Simple. Implement the suggestions given to you so far. By doing that, you will find the code easier to read; there will be less bugs, and people will be more inclined to help you. It's for your own sake as a programmer as much as ours.
    antred likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    I'm sorry. This course is really frustrating. Thank you for your answers! I'll figure it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trouble with simple arrays
    By time4f5 in forum C Programming
    Replies: 10
    Last Post: 07-11-2011, 07:09 AM
  2. Simple program on Arrays
    By suryak in forum C Programming
    Replies: 6
    Last Post: 04-10-2011, 06:29 AM
  3. Simple arrays
    By DerrickakaDRoC in forum C Programming
    Replies: 12
    Last Post: 12-12-2010, 09:57 PM
  4. A simple program using ARRAYS, STRUCTS, and CLASSES
    By CStudentHelp in forum C++ Programming
    Replies: 8
    Last Post: 04-17-2008, 09:57 PM
  5. Simple question about arrays
    By tima in forum C Programming
    Replies: 3
    Last Post: 05-21-2007, 09:24 AM

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