Congratulations to AcaCandy on her 100,000th post!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen blue screen boot bsod computer connection crash css dell display drive driver drivers email error ethernet excel firefox firefox 3 game hard drive internet internet explorer itunes laptop linux malware monitor network networking nvidia outlook outlook 2003 outlook 2007 outlook express partition problem router slow software sound trojan usb video virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Embarrased To Ask


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
Kramer55's Avatar
Computer Specs
Senior Member with 588 posts.
 
Join Date: Jan 2005
Location: Michigan
Experience: Intermediate
19-Jan-2005, 09:49 PM #1
Embarrased To Ask
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

Last edited by Kramer55 : 19-Jan-2005 10:04 PM.
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
20-Jan-2005, 12:29 AM #2
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.
__________________
10 ? "a line as the unending horizon"
20 ? "a curve as the rolling hillside"
30 ? "a point as a distant bird"
40 ? "a ray as the rising sun"
run
Fyzbo's Avatar
Senior Member with 1,777 posts.
 
Join Date: Feb 2002
Location: North Carolina, USA
Experience: Programming-Advanced|EVER
20-Jan-2005, 12:46 PM #3
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).
Kramer55's Avatar
Computer Specs
Senior Member with 588 posts.
 
Join Date: Jan 2005
Location: Michigan
Experience: Intermediate
20-Jan-2005, 04:12 PM #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?
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
21-Jan-2005, 07:11 AM #5
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.
Roshith's Avatar
Senior Member with 393 posts.
 
Join Date: Nov 2004
Location: Dubai
Experience: know a little...learning lots more...
21-Jan-2005, 07:37 AM #6
....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
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
21-Jan-2005, 07:50 AM #7
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.
Kramer55's Avatar
Computer Specs
Senior Member with 588 posts.
 
Join Date: Jan 2005
Location: Michigan
Experience: Intermediate
21-Jan-2005, 03:21 PM #8
wow, thanks

i cant believe i forgot how to do that
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:53 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.