Arrays help with loops

I understand arrays is a data storage of #
the problem im having is using it with loops and if, else

say user input 10 numbers that are negtive or posive number and if they enter a character or 0 it ends the program. when it ends the program it prints the stored array number that the user entered.

how would i end the if statement if user entered a character or 0 and return it to another function to print it out?

i know how to give error message if they enter a 0 or character but how do u end the if statement beside the else statement?



You do not need the if..else.

A loop has a condition that determines whether it is executed/repeated. For example:
1
2
3
4
5
6
7
8
constexpr size_t N = 10;
size_t counter = 0
while ( counter < N ) {
  // something
  ++counter;
}

// this is "at the end of the program" 

The something above is repeated for N times (unless the something modifies the counter).

The condition is counter < N

One can use std::cin in a condition. It is true if the stream is ok.
Formatted stream input operator returns the stream.
Therefore, std::cin >> foo can be used in a condition. It has a side-effect of storing a value into variable foo.

More than one conditional expression can be combined with logical AND and logical OR.

In condition foo and bar and gaz the foo is evaluated first.
If foo is false, the whole condition is false. Else the bar is evaluated.
If bar is false, the whole condition is false. Else the gaz is evaluated.
If gaz is false, the whole condition is false. The condition is true only if all three subconditions ( foo, bar, gaz) are true.

For example: (counter < N) and (std::cout << "Give an integer, (0 for quit)\n") and (std::cin >> input) and (0 != input)
* ends loop if N inputs have already been taken (without asking for more input)
* also ends loop if output fails (unlikely)
* also ends loop, if user writes a character and input is int
* also ends loop if that successful input value is 0


... but you can use the if..else:

1. Keyword statements break; and continue; can be used within a loop.
1
2
3
4
5
while ( cond1 ) {
  if ( cond2 ) break;
  if ( cond3 ) continue;
  // statements
}

If that cond2 is true, then the execution of the loop ends.
If that cond3 is true, then the the rest of the body (the statements) are skipped and execution continues from line 1, testing cond1 again.

2.
1
2
3
4
5
6
7
8
while ( cond4 ) {
  if ( cond5 ) {
    // make cond4 false
  }
  else {
    // statements
  }
}

If that cond5 is true, then statements (line 6) are skipped.
Since line 3 modifies what will be evaluated in cond4, the loop ends at this iteration.
Thank you so much for your time to write all that info out. im currently using cstdio library so im not sure about std library and using the cout.

when i tried using the break it ends the program if they enter a c, or pos or neg # and when i enter a 0 it just keeps on looping.

what i want is if they enter a number 0 or a character like foo it ends the program and goes to my void function to print out what they enter earlier before typing in 0 or foo.

im having problem figuring the user input section
this is the fuction that the user input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int GetInFo(int a, int b)
    int User=0;
    int Read=0;
    do
    {
    Read=scanf("%d", &User);
    if (Read<1 || User!=0)   // im not sure it this is right? 
    {
     break;
    }
    }
     while ((Read<1 || (User<a) || (User!=0) || (User>b);
     return User;
}
Last edited on
Topic archived. No new replies allowed.