Question:

Basic interger reverse c++ help?

by  |  earlier

0 LIKES UnLike

include <iostream>

using namespace std;

int main()

{

int x[10];

int i;

cout << "Type in your 10 numbers" << endl;

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

cin >> x[i];

while (x != 0)

++i;

--i;

cout << endl;

cout << "The Numbers reversed are:" << endl << endl;

while (x != 0)

{

cout << x;

--i;

}

cout << x;

cout << endl;

return 0;

}

What's wrong with my program it will not Display the reverse intergers.? Please Help

 Tags:

   Report

3 ANSWERS


  1. There are a few errors. These two lines

    while (x != 0)

    ++i;

    will just cause an infinite loop. You declared x as an array. So x !=0 is always true. Incrementing i does not change this. So remove these two lines

    Also this bit

    while (x != 0)

    {

    cout &lt;&lt; x;

    --i;

    }

    cout &lt;&lt; x;

    makes no sense. You declared x as an array so you must address it as x[i]

    I think you want to change this bit to

    while (i != 0)

        {

            cout &lt;&lt; x[i];

            --i;

        }

    cout &lt;&lt; x[0];

    Then it should work


  2. if you are forced to use both FOR-loop and WHILE-loop:

    #include &lt;iostream&gt;

    using namespace std;

    int main()

    {

    int x[10];

    int i;

    cout &lt;&lt; &quot;Type in your 10 numbers&quot; &lt;&lt; endl;

    for (i=0; i&lt;10; i++)

    cin &gt;&gt; x[i];

    cout &lt;&lt; endl &lt;&lt; &quot;The Numbers reversed are:&quot; &lt;&lt; endl &lt;&lt; endl;

    while (i &gt; 0)

    {

    cout &lt;&lt; x[--i];

    }

    cout &lt;&lt; endl;

    return 0;

    }

    &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; OR &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;

    if you are free to choose which loops to use:

    #include &lt;iostream&gt;

    using namespace std;

    const int ENTRIES = 10; //use constants!

    int main()

    {

    int x[ENTRIES];

    cout &lt;&lt; &quot;Type in your 10 numbers&quot; &lt;&lt; endl;

    for (int i = 0; i &lt; ENTRIES; i++)

    cin &gt;&gt; x[i];

    cout &lt;&lt; endl &lt;&lt; &quot;The Numbers reversed are:&quot; &lt;&lt; endl &lt;&lt; endl;

    for(int i = ENTRIES - 1; i &gt;= 0; i--)

    cout &lt;&lt; x[i];

    cout &lt;&lt; endl &lt;&lt; endl;

    return 0;

    }

  3. &quot;while (x != 0)

    ++i;

    --i;&quot;

    what is that for???

    &quot;while (x != 0)

    {

    cout &lt;&lt; x;

    --i;

    }

    cout &lt;&lt; x;&quot;

    and that???

    for (i=9; i&gt;=0; i--)

    cout &lt;&lt; x[i];

    will display the numbers reversed.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions