Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
Photo

Reversing order


  • Please log in to reply

#1
Legally_Insane

Legally_Insane

    New Member

  • Member
  • Pip
  • 1 posts
I need help with my C++ code. The program is suppose promp the user for 6 letters then output it in reversed order. My code doesn't have any errors and runs but it doesn't do anything. Can anyone help with what could be wrong with it.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <iomanip>
int main()
{

	const int arraysize=6;
	int i=0;
	char a[6];
	cout<<"Enter "<<arraysize<<" letters: ";
	 cin>>a[i];
	 for(i=0; a[i]; i++)
	 {

	 for (i=0; a[i]; i--);
	 }
	 
return 0;
}

  • 0

Advertisements


#2
Swandog46

Swandog46

    Malware Expert

  • Member
  • PipPipPipPip
  • 1,026 posts
  • MVP
It doesn't *do* anything. You haven't TOLD it to do anything. Look at this:

for(i=0; a[i]; i++)
{

for (i=0; a[i]; i--);
}

First of all, that loop is dangerous because you are in essence asking the compiler to dereference memory outside of what you have allocated (the 6 characters). If the memory after a[6] is not null (zero), then this loop will never terminate.

Second, you don't tell it to do anything once it is looping.....
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP