Click here to Skip to main content
14,937,945 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, I am making a program that has the user enter 9 numbers, 0-8 is what I am intending to be entered, and I want to make sure that:
1) they don't enter something that isn't a number {if it isn't a number the program asks the user to try again}
1a) if it is a number it is between 0-8
1b) that none of the numbers repeat {no duplicates of the same number}
The numbers can be in any order, the code will take the 9 numbers and make it into a 3x3 grid and attempt to order it as the user wants it once they entered as the next 9 numbers, still 0-8. What I have so far is that the code checks if the values entered, one at a time, are numeric and if it is it proceeds to ask for the end goal. The final code will be using a Breadth-first search algorithm to shift the numbers from point a to point b.

Example: User enters 283104765 (first 3 numbers is first row and so on) as the starting grid and 813204765 as the ending grid.


Right now, this snippet of codes only objective is to prevent anything that isn't the numbers 0-8 from being entered and for those entries to not have duplicates.

#include <iostream>
#include<limits>
using namespace std;

template<typename T, size_t n>
void print_array(T const(& arr)[n])
{
    for (size_t i = 0; i < n; i++) {
        std::cout << arr[i] << ' ';
    }
}

int main() {
    int startInt[9], endInt[9];
    int counter;
    cout << "Please enter numbers 0-8 in any order to start:";
    for(counter=0;counter<9;counter++){
        cin>>startInt[counter];
            while(1)
            {
                if(cin.fail())
                {
                    cin.clear();
                    cin.ignore(numeric_limits<streamsize>::max(),'\n');
                    cout<< "You have entered wrong input. Please enter 9 numeric values that range between 0-8 wthout using a number twice:";
                    cin>>startInt[counter];
                }
                if(!cin.fail()){
                    //do nothing
                }
                break;
            }
        }
        cout << "The array is: "; print_array(startInt);
       cout << "Please enter numbers 0-8 in any order to solve:";
    for(counter=0;counter<9;counter++){
        cin>>endInt[counter];
            while(1)
            {
                if(cin.fail())
                {
                    cin.clear();
                    cin.ignore(numeric_limits<streamsize>::max(),'\n');
                    cout<< "You have entered wrong input. Please enter 9 numeric values that range between 0-8 wthout using a number twice:";
                    cin>>endInt[counter];
                }
                if(!cin.fail()){
                    //do nothing
                }
                break;
            }
        }
        cout << "The array is: "; print_array(startInt);
}


What I have tried:

Originally I had the code entered be a string with all variables entered together, but that could be anything so I changed it into an integer array of [9], which works. But I need to convert that back into a string, without any spaces in-between them. The code so far works, the originally code where it was using strings instead of arrays and wasn't checking the submissions. However, I want to prevent the code for having errors so I need to add to it so I started doing that in another file to ensure this part works before merging them together. To ensure that the arrays contained the proper data I made void print_array.
Posted
Comments
Gerry Schmitz 30-Apr-21 19:58pm
   
Just tell them to enter a comma separated list of numbers as a string. Then scan the string as a character array, and report an error if any character is not a digit or a comma. Split the (valid) string on comma to get a series of numbers. If the result is more than 9 numbers (9+ commas), report an error.
Greg Utas 1-May-21 10:04am
   
Do you have a question, or do you just want feedback on your code? Here's some advice anyway. You have two almost identical code segments, one populating startInt, and the other, endInt. Create a function to do this and pass it the array to populate. Copy-pasting code is usually a bad idea because you have to remember all the clones when fixing a bug.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900