How close am I??

Hey guys, heres my assignment:

Write a Program that reads the integers between 1 and 100 and counts the occurrence of each number. Assume the input ends with 0.

**Note** if a number occurs more than one time, the plural word "times" is used in the output.


and heres the code I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main (void)
{
    string num = "";
    char search = '0';
    int i = 0;
    int count = 0;
    
    cout << endl << "Enter Number: ";
    cin >> num;

    cout << endl << "Enter Search Value: ";
    cin >> search;
    
    for (i = 0; i < num.length(); i++)
    {
        //cout << num[i] << endl;
        if ( num[i] == search )
        {
             count++;
        }
    }
    
    cout << endl << "Number of occurences: " << count << endl;
    
    system("PAUSE");
    return 0;
}



How close am I to getting this code correct?
I think you are not very close.
I interpret the assignment like this:
Keep reading integers, if the input is less than 1 or greater than 100 it's invalid, if it's 0 stop.
Each time you read a value increase a counter specific to that value ( hint: array )
After you finished reading from input output the value of the counters
Is there anyone who can help me write this code???

I have to take this Intro to Comp Science class so I can get into this HTML class over the summer!! Any extra help would be absolutely appreciated! Thank you
closed account (zb0S216C)
Is there anyone who can help me write this code???

Did you even read Bazzy's reply?

I'm not going to give you a full solution. Here's a hint for you though: An array of 100 and plus-plus.
Last edited on
Don't ask for explanations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

#define max_amount_of_numbers 100

#define biggest_allowed_number 100
#define lowest_allowed_number 1

#define end_of_input "0"

int main ()
{
    string num[max_amount_of_numbers];
    string search;

    cout << endl << "Enter Numbers: " << endl;

    int i = 0;
    while(cin>>num[i]){
      if(num[i]==end_of_input) break;

      if((atoi(num[i].c_str())>=lowest_allowed_number)&&(atoi(num[i].c_str())<=biggest_allowed_number))
        i++;
      else
        cout << "Imput out of range ("
             << lowest_allowed_number << " - " << biggest_allowed_number
             << "). Please try again"
             << endl;
    }

    cout << "Enter the search value: ";
    cin  >> search;
    cout << endl;

    int count = 0;
    while(i>-1){
      if( num[i].compare(search)==0 )count++;
      i--;
    }

    if(count==0)cout << "Given value did not occur.";
      else if(count==1)cout << "Given value occurred 1 time.";
        else cout << "Given value occurred: " << count << " times.";


    cin.get();
    return 0;
}
Last edited on
closed account (zb0S216C)
Forget strings all together. He doesn't need one. Madredcake, the example you gave is way too much for a simple program.
1. HE WAS DOING it on strings, probably only becouse he didn't know how do this but ....
2. His code shows different problem from the one he writes about. (Counting occurrences of one chosen number, not each one from input). I am not even sure what he wants to do.
Last edited on
Whatever happened to "we don't post full solutions"?
closed account (zb0S216C)
Madredcake, he was struggling with a simple program and you post a full solution with methods that he will not yet understand. Just saying as all.
Last edited on
That rule is always being subverted by newcomers. I think we should put Albatross's thread into this section. With very big flashing letters. Oh, and the first time you attempt to reply to a thread it should be opened in a new tab.
If the question is same as Bazzy deduced, I think array is more than enough for this problem


Thanks for all you help guys. The code is still not working though. I know that I am asking for alot of help here but its not like I'm a Computer Science major. I am a Graphic Designer and Video Producer who just needs some help on this assignment. Thanks for all those helping and to those of you upset, I apologize but if any of you ever had a question about Graphic Design, I'd be right there to help too.

you could save 1~100 in an array
when the user input the data
just map the number of the data to the appropriate location of the array
and increment the value of the array at that location
closed account (zb0S216C)
when the user input the data
just map the number of the data to the appropriate location of the array
and increment the value of the array at that location

That's exactly what I had in mind :)
Topic archived. No new replies allowed.