?
Solved

need help with a loop and aray

Posted on 2004-10-19
3
Medium Priority
?
274 Views
Last Modified: 2010-04-01
i neeed help writting a program that asks the user to input 10 integers tests to make sure it is an integer and assorts it in assending order if they ener a invalid number ask them to reenter it

i need help with this and it is urgent so anyone that can help me with this i would gatelly appreciate it

maybe some hints or some code on how i should go about this

thanks
ted
0
Comment
Question by:tedschnieders
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12349570
#include <list>
#include <iostream>
using namespace std;

int ReadInt () {

    int n;
    string str;
      while (1) {

        cout << "Enter an integer: " << endl;

          getline(cin,str);

        char* pc;
            n = (int) strtol(str.c_str(),&pc,10);

            if (*pc) { // conversion error, not a valid int

                cout << "Invalid number" << endl;

            } else break; // terminate loop
      }

      return n;
}

int main () {

    list<int> lst;
    int n;

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

          n = ReadInt ();

            lst.push_back(n);
      }

      lst.sort();

      for ( list<int>::iterator it = lst.begin(); it != lst.end; ++it) {

          cout << *it << endl;
      }
}
0
 

Author Comment

by:tedschnieders
ID: 12349737
thank you for responding so quickly

i want them to enter one number at a time and store them in a array 0 - 9
then i want to spit them back out in acccending orplease help me
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 12349889
Oh, in this case, you could use

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

int compare( const void *arg1, const void *arg2 ) {

    int n1, n2;
      
      n1 = (int) arg1;
      n2 = (int) arg2;

      if ( n1 < n2) return -1;

      if ( n1 > n2) return 1;

      return 0;
}

int ReadInt () {

    int n;
    string str;
      while (1) {

        cout << "Enter an integer: " << endl;

          getline(cin,str);

        char* pc;
            n = (int) strtol(str.c_str(),&pc,10);

            if (*pc) { // conversion error, not a valid int

                cout << "Invalid number" << endl;

            } else break; // terminate loop
      }

      return n;
}

int main () {

    int arr[10];
    int n;

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

          arr[i]= ReadInt ();
      }

      qsort(arr,10,sizeof(int),compare);

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

          cout << arr[i] << endl;
      }
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question