a Counter of some sort?


I'm trying to code a sample project (of which I have no examples of, nor any test data examples) and I'm baffled at where to begin. I have to key in a random (potentially infinite) number of integers and hit enter after each one. When I'm done entering numbers I would then type in the word "Done". How do I account for an unknown number of numbers? Normally wouldn't an integer have a variable assigned to it?
Any thoughts, suggestions or snippets to give an example of what this means?

Thanks!!

Comments

  • This is where dynamic memory allocation is suitable. malloc() and free() in C, new and delete in C++. Though if you are using C++, the best solution is to use the vector class from STL.
  • Unfortunately, I'm using plain old C. I "Dogpiled" those and couldn't find results of their usage in the proper context.
    I'm finding that learning on my own is a bit difficult, but I appreciate forums such as this one where there are those that are willing to help with the basics. Thank You All!
  • int total = 0;
    int intCounter = 1;
    int userInt = 0;

    cout << "Enter integers!

    Type -1 when you're finished entering all integers!

    ";

    while (userInt != -1)
    {
    cout << "Please enter integer " << intCounter << ": ";
    cin >> userInt;

    if (userInt != -1)
    {
    total = total + userInt;
    intCounter++;
    }
    }

    cout << total << endl;
  • I'll try that out, thanks!
  • Good luck!
    http://www.proskimboarding.com - Skim SHOP, Tips & Tricks, Forums, and Other Resources
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories