-3

A segment of the program is instead of crashing when invalid input is entered it should just give a warning when the input is zero, negative or a fractional number and keep count of the errors that were entered. I am not sure how to keep count of the errors. Here is my code. numOfJars[] is an array to store the number of jars sold as ints. If user enters a fractional number it should store it as int in array.I am still new to programming and any help would be appreciated.

    cin >> num;

    //If negative number entered, it should be stored as zero
    if ( num < 0)
    {
        num == 0;
    }

    // Keeping count of invalid values entered program segment
    if ( num == 0 || num < 0 || !(cin >> num))
    {
          // Not sure what to put here

    numOfJars[index] = static_cast<int>num;


}`
Pisancho
  • 1
  • 1

1 Answers1

0

@Pisancho: This looks like a homework question, and people on StackOverflow are very harsh on students who ask such questions. You are expected to learn the concepts of programming and ask questions here only when it is something that is very difficult to solve. What you've asked here is a very simple problem.

There are two things to understand here.cin is used for taking inputs from the user. So you shouldn't use it inside that if () statement.

Think about it like this: If someone wrote a number on a sheet of paper and handed it to you, how would your brain analyze if it is an invalid number? You would do it in steps.
Step 1: Accept the sheet of paper and view it.
Step 2: Check if the number is 0 or less than 0. If it is, then remember in your brain that there is one invalid number. If you had counted invalid numbers earlier, then increment that count.
Step 3: Check if the number is a fraction. If it is not, then increment the count in your brain.
Step 4: Store the number in the array.
Step 5: Goto Step 1.

Did you notice what the above steps are? They are an algorithm. Now you just have to know how to convert the algorithm into code. For that, you have to know the grammar of the computer. Called as "syntax".

I won't give you the actual C code. That's for you to figure out. I'll give you some pseudo-code, and it's upto you to figure out how to convert it into proper C code and to also understand the concepts and learn more about programming.

while(true) {
int[] myArray;//make sure you initialize this array correctly later
float inputNum;//a float is capable of storing a fraction number 
int countInBrain = 0;//start with a zero count. We don't need count to be a fraction, so we use int
int arrayIndexToStoreNumber = 0;
cout<<"Enter a number \n";//It always helps to let the user know what to enter
cin >> inputNum;

if (inputNum==0.0f || inputNum < 0.0f || inputNum % 1 != 0) {
countInBrain = countInBrain + 1;
}
else {
myArray[arrayIndexToStoreNumber] =  inputNum;
arrayIndexToStoreNumber++;
}
} 

So you see...you have to think of all the variables you need to store. Be careful with float comparisons (find out why). Also find out why I added f to the end of 0.0.

Now go back to your books and learn C++ syntax properly. You'll need it. Personally, I dislike the fact that C++ is one of the first languages taught in schools. They should start with something like Python, so that students can learn syntax and logic first. Anyway. All the best.

Community
  • 1
  • 1
Nav
  • 19,885
  • 27
  • 92
  • 135