user8879160
user8879160

Reputation: 13

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;
        }
    }

Upvotes: 0

Views: 743

Answers (4)

Caleth
Caleth

Reputation: 63039

You just have to have some state to see if you have seen 42 already, and only output if you haven't

#include <iostream>

int main()
{
    bool output = true;
    for (int n; std::cin >> n;)
    {
         output &= (n != 42);
         if (output)
         {
             std::cout << n << std::endl;
         }
    }
    return 0;
}

Upvotes: 0

Oneiros
Oneiros

Reputation: 4378

Use a bool value to control the execution of your code.

#include <iostream>
#define N_INPUT 100
#define THE_ANSWER 42

using namespace std;

int main()
{
   int array[N_INPUT], i, input, count=0;
   bool universeAnswered = false;
   for (i = 0; i < N_INPUT; i++) {
      cin >> input;
      if (!universeAnswered)
      {
         if (input == THE_ANSWER) {
            universeAnswered = true;
         } else {
            array[count] = input;
            count++;
         }
      }
   }
   for (i = 0; i < count; i++) {
      cout << array[i] << endl;
   }
}

(My code was not tested)

Upvotes: 0

R2K
R2K

Reputation: 56

Pretty sure the easiest way to do so is to simply ask the user how many numbers they need to enter.

#include <iostream>
using namespace std;

int main()
{
    int A[100], k, count;

    cout << "How many numbers do you want to enter ? ";
    cin >> count;   //this is to count how many numbers the user wants to enter

    for(int i(0); i < count; ++i) //put all the numbers user enters in your array
    {
        cin >> k;
        A[i] = k;
    }

    cout << endl;

    for (int i(0); i < count; ++i)
    {
        if (A[i] == 42)             //if the element at index i is == 42 then stop displaying the elements
            break;
        else
            cout << A[i] << " ";    //else display the element
    }

    cout << endl;
    return 0;
}

Else you would need to put everything in a string and parse it and i'm not quite sure how that goes as I am a beginner as well.

EDIT: Actually here you go, I think that is correct and does exactly what you want. Do keep in mind that if user enters p.e "1 88 442" it will output "1 88 4" because it found "42" in "442". But it should be okay because you precised input numbers should only be two digits max.

#include <iostream>
using namespace std;

int main()
{
    string k;
    getline(cin, k);

    cout << endl;

    for (unsigned int i(0); i < k.length(); ++i)
    {
        if (!((k[i] == '4') && (k[i+1] == '2'))) //if NOT 4 followed by 2 then display
            cout << k[i];
        else
            break; //else gtfo
    }

    cout << endl;
    return 0;
}

Upvotes: 0

Srikanth Chekuri
Srikanth Chekuri

Reputation: 2274

You don't have to use array at all. You can print the value just after reading it. Exit when you read 42. This may help you.

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    int n ;
    for(; ;) {
        cin >> n ;
        if(n == 42) {
            return 0 ;
        }
        cout << n << endl ;
    }
    return 0;
}

Upvotes: 1

Related Questions