Arrays

This is a discussion on Arrays within the C++ Programming forums, part of the General Programming Boards category; Hi, I would like to know were my error(s) are in this program. Could somebody look and help me out? ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    Arrays

    Hi,
    I would like to know were my error(s) are in this program. Could somebody look and help me out? I'm trying to get a string and reverse the order using for loops.
    Code:
    #include <iostream.h>
    #include<string.h>
    
    //void ReverseIt(char [], char []);
    
    int main()
    
    
        {
        	
        	char str[50], copy[50];
    		int i,j,length;
    		length = strlen(str);
    
    		cout<<"\nEnter a string: "<<endl;
    		cin.getline(str,49);
    
    		cout<<"\nYou entered " << str <<" !" << endl;
    
    		for(i=0;i<50;i++)
    		{
    			cout<<"\nYou entered " << str[i] << endl;
    
    			for(j=0;j>50;j--)
    			{
    				cout<<"\nThe reverse is " << copy[j] <<endl;
    			}
    		}
    		return 0;
    }

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    I'll point out just one error..

    for(i=0;i<50;i++)

    i should go up to length, not 50. If the string is less than 50 characters, you are going to go into uninitlized memory.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    Like wise, this is wrong:

    for(j=0;j>50;j--)

    Its going from 0 and down, until its greater than 50? Hmm, I think that will take some doing... (Should start at length and down to 0.)

    Also, the second for loop should _not_ be inside the first one. Why display the reversed string after every character of the initial string?
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    2
    <Also, the second for loop should _not_ be inside the first one. Why display the reversed string after every character of the initial string?> I didn't mean for that to happen...I just don't know what I'm doing and I'm trying to learn. I'm trying to take the string that is entered and display it as well as display the same string backwards.

  5. #5
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    change
    Code:
    length = strlen(str); /* here you get the lenth before its entered by the user resulting in an incorrect lenth */
    cout<<"\nEnter a string: "<<endl;
    cin.getline(str,49);
    to
    Code:
    cout<<"\nEnter a string: "<<endl;
    cin.getline(str,49);
    length = strlen(str); /* here you get the actual length of the string the user has entered */

  6. #6
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    ...and here's your fixed loop(s)
    Code:
    for(i=0;i<length;i++)
    {
    	cout<<"\nYou entered " << str[i] << endl;
    }
    
    for(j=length;j>length;j--)
    {
    	cout<<"\nThe reverse is " << copy[j] <<endl;
    }

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    You also try to print "copy" without assigning the values of "str" to it. And use better spacing. This looks bad:

    for(i=0;i<len;i++)

    Try instead:

    for(i = 0; i < len; i++)

    or even:

    for( i = 0; i < len; i++ )

    Code:
    #define MAX 50 /*..now we just change this instead of editing program later ;) */
    
    int main() {
    
    char str[MAX], copy[MAX];
    int i, j, length;
    
    
      do{
    
       cout<<"\nEnter a string, type 'q' and ENTER to quit: "<<endl;
    
       cin.getline(str, MAX);
    
       length = strlen(str);
    
       cout << "\nYou entered: " << endl;
    
         for(i = 0; i < length; i++)
         {
          cout << str[i] << endl;
         }
    
      cout << "\nThe reverse is " <<endl;
    
        for(j = length-1; j >= 0; j--)
        {
          cout << str[j] <<endl;
        }
       
     }while(str[0] != 'q');
    
    return 0;
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  8. #8
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    nah, I liked my version better

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 06:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 03:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 09:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 08:24 PM

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