Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
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