-2

First of all thanks for answering and helping out...
Now i was making a program to let the user enter any number... and then use the program to point out the total number of 4's in the the number entered and i have now encountered a problem..

This is my first post here so please excuse me if i make any mistakes..

The Code

int main()
{
    int T,i,j,l;
    char N,p[10];
    cin>>T;
    while(T--)   //The number of times a user can enter a new number
    {
        cout<<"\nEnter Numbers\n";
        l=0;i=0;
        do
        {
            N=getch();  //getch is used so that the enter key need not be pressed and the 
                        //number looks like a whole and also so the each number is 
                        //individually stored  
            p[i]=N;    //here the number entered is stored in p
            cout<<N;  //to display the number obviously
            l++;i++;
        }while(N!=' ');  //Now here between '' something has to be present so that the loop 
                         //terminates as soon as the enter key is pressed right now as soon 
                         //as the spacebar is hit the loop will terminate. 
        cout<<"\n";
        j=0;
        for(i=0;i<l;i++)  //using l so that the loop runs accordingly
        {
            if(p[i]=='4') 
            {
                j++;    //for couting the number of 4's
            }
            cout<<p[i]<<"\n";   //wont be needing in the final program but here cout is just
                               // to check the output
        }
        cout<<"\n THERE ARE "<<j<<" FOURS\n";
    }
}


Please not that i already have a solution for my program so please DO NOT provide a different code using some different logic... i really need this very same program to work.i know that this program can be made to work using string length but here i want the loop to terminate after the enter key is pressed.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • @DrZoo can u do this? – Tejas Singh Bedi Jul 17 '15 at 15:50
  • Why would you not want to ability to be shown different logic? Another way could be much more efficient and a method you'd want to replicate down the road – Jeff S Jul 17 '15 at 15:50
  • @JeffS thanks for responding .. i know another logic.. actually i was very keen to know if the enter thingy would work ..but seems difficult ... i am open to new ideas so yeah another logic yeah why not.. – Tejas Singh Bedi Jul 17 '15 at 15:53

2 Answers2

2

Well if you want to stop getting input when the user presses enter instead of space you need to test against '\r', '\n' or '\r\n' depending on what OS you are using. That said you really should be using standard C++ if you are going to use C++. You could easily make your code like:

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    int loops;
    std::cout << "How many numbers to check: ";
    std::cin >> loops;
    std::cin.get(); // eat newline
    for (int i = 0; i < loops; i++)
    {
        std::string numbers;
        std::cout << "Enter numbers and press enter: ";
        std::getline(std::cin, numbers);
        auto numberOf4s = std::count(numbers.begin(), numbers.end(), '4');
        std::cout << "Number of 4's entered: " << numberOf4s << std::endl;
    }
    return 0;
}

Live Example

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
0

You can to see check if N is equal to '\r'. So your while loop looks like

    do
    {
        N=getch();  //getch is used so that the enter key need not be pressed and the 
                    //number looks like a whole and also so the each number is 
                    //individually stored  
        p[i]=N;    //here the number entered is stored in p
        cout<<N;  //to display the number obviously
        l++;i++;
    }while(N!='\r');
okebz
  • 132
  • 1
  • 8