vendredi 3 novembre 2017

Controlling input in c++ and display

Question : Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

Example Input: 1 2 88 42 99

Output: 1 2 88

So that is the question, however i am still a beginner and unable to have an input tab like that. In my program, how should i modify it such that it still accepts numbers after 42, however, it does not print them? currently I am only able to terminate the input at 42.

#include <iostream>
using namespace std;
int main()
{
    int A[100], num, i=0,k,count;
    for(count = 0; count != 1;){
            cin >> k;
        if (k!=42){
            A[i] = k;
            i++;
    }
        else
            count =1;
    }
        cout << endl;
    for (count = 0; count <i; count ++){
        cout << A[count] << endl;
        }
    }

Aucun commentaire:

Enregistrer un commentaire