Tech Support Guy banner
Status
Not open for further replies.
1 - 8 of 8 Posts

· Registered
Joined
·
588 Posts
Discussion Starter · #1 ·
Over the summer, I began to teach myself C++ programming.

But when school started (7 months ago), I havent done any programming, and I have forgotten the basics, whereas I still maintain knowledge of classes and arrays.

Anyway, (still cant believe I forgot how to do this)
How would I make a simple program (without classes, just simple code)
that would take input from the user(numbers) and as the output display how many numbers were entered?

would it be something with a for loop?

Thanks for any help
 

· Registered
Joined
·
2,640 Posts
Do you mean, have the user input an integer and then count how many digits?

If so, you'll want to input it into a string, validate it, and then count the length.

string input;
getline(cin,input);

and go from there.

Now, if you just want the user to keep entering integers till they choose not to enter anymore, you'd use a for loop or while loop and just increment an integer (starting at 0) so you can display how many times the user entered an interger when the loop ends.

If you need to store each entry, you'd use a vector and then you could just display the size() to get how many entries.

If you specify exactly what you want, that would help.
 

· Registered
Joined
·
1,984 Posts
Use a while loop and set up an escape character, that way when the user enters the escape character the loop ends. Inside the loop just cin the numbers and do what you want with them, add them or or run a count of how many time you go through the loop(not sure what you meant).
 

· Registered
Joined
·
588 Posts
Discussion Starter · #4 ·
so something like:

Code:
int counter=0
cout << "Enter a number greater than or = to 0" << endl;
cin >> number

if(x >= 0)
{
cout << "Enter another number greater than or = to 0" << endl;
cin >> number
counter++
}
i know (besides not having an escape function) that I am missing one major part, but i can figure it out

does someone know?
 

· Registered
Joined
·
2,640 Posts
This will give you an idea.

Code:
#include <iostream>

using namespace std;

int main() {
    size_t c = 0;
    for (bool done = false; !done;) {
        cout << "\n" << "Enter a number greater than or equal to zero: ";
        int number;
        cin >> number;
        if (number >= 0) {
            ++c;
        } else {
            done = true;
        }
    }
    cout << "\n" << "You entered a valid number " << c << " times." << endl;
}
Just keep in mind that when you use cin to directly input into in integer, if you enter a bunch of letters etc., the program will crash and loop forever.
 

· Registered
Joined
·
401 Posts
....heres something similar to what shadow2531 said....with a few very small diffrences...

#include<iostream.h>
void main()
{
int count=0,i;
cout<<"Input an integer greater than or equal to 0";
cin>>i;
while(i>=0)
{
count++;
cin>>i;
}
cout<<"The number of integers entered is "<<count;
}

i think this mite be a little more easier for u to understand....and like
Shadow2531 said....if u enter anything other than and integer....the program will crash
 

· Registered
Joined
·
2,640 Posts
For future reference, to ensure you get a valid and safe integer, you can use the regex boost library. www.boost.org

Code:
#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
#include <boost/regex.hpp>

int get_int() {
    std::string in;
    for (bool valid = false; !valid; ) {
        getline(std::cin,in);
        if (boost::regex_match(in, boost::regex("-[0-9]+")) && in.size() < 11) {
            valid = true;
        } else if (boost::regex_match(in, boost::regex("[0-9]+")) && in.size() < 10) {
            valid = true;
        } else {
            std::cout << "\n" << "Invalid integer. Try again: ";
        }
    }
    return boost::lexical_cast<int>(in);
}

using namespace std;

int main() {
    size_t c = 0;
    for (bool done = false; !done;) {
        cout << "\n" << "Enter a number greater than or equal to zero: ";
        int number = get_int();
        if (number >= 0) {
            ++c;
        } else {
            done = true;
        }
    }
    cout << "\n" << "You entered a valid number " << c << " times." << endl;
}
You can of course manually do the check with the STL, but that's not as much fun. :)
 
1 - 8 of 8 Posts
Status
Not open for further replies.
Top