Help! dont know the problem

I am trying to make a program that counts how many times you enter it.Trying to make it so you can enter any number of integers then press enter or by pressing x to make it count the integers and list

what should i change the arr to?
code:
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
#include <iostream>
#include <stdio.h>

int main(int argc, char *argv[]) {
  int arr[5] = {0};
  int i=0;
  int x=0;
  
  printf("Enter ten integers:\n");
  
for (i=0; i<5; i++) {
    scanf("%d", &x);
    if(x >= 0 && x <= 57) arr[x]++;
  }
  //below is ok and right
   for (i=0; i<57; i++) {
    printf("%d seen %d times\n", i, arr[i]);
  }
  
  

system("pause");
    return 0;
	
}



the problem is that its the output is showing nothing right and that its - high number and high number and low number.



Can someone help me.
You'll either need to use vectors or create a dynamic array. Judging by your code, I would assume you're just getting started with this, so I would do some research on vectors. Arrays are limited to the size you give them when you declare them, thereby making them incompatible with programs that may have a larger or smaller array than you initially intended.

Vectors can shrink and grow in size as the user enters data, making them perfect for this type of program.

Happy coding.
so take out the array and switch it with vectors?


i got advise from JLBorges to use something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <sstream>

int main()
{
    std::string line ;
    std::getline( std::cin, line ) ; // 

    std::istringstream stm(line) ;
    const char COMMA = ',' ;
    int number ;
    char delimiter ;
    int count = 0 ;
    while( stm >> number )
    {
        ++count ;
        if( stm >> delimiter && delimiter==COMMA ) continue ;
        else break ;
    }
    
    std::cout << count << '\n' ;
}




but this seems a little to upper for me.

Wait, is this supposed to count how many times a specific integer appears in an array and/or vector? Your code takes a different approach to how I would do that.

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
#include <iostream>
#include <vector>

using namespace std;

int main() 
{

     vector<int> myVector;
     int input,
         size = 0, // size of vector
         i = 0;

     cout << "Enter a list of integers (-1 to stop): ";
     cin >> input;

     //Fill Vector EDIT: I accidentally put array, sorry...
     while (input != -1) {
          vector[i] = input;
          i++;
          size++;
          cin >> input;
     }

     int search;
     int total = 0; // total appearances of 'search'
     cout << endl << "What number would you like to search for? ";
     cin >> search;

     //Print Number of Appearances of Integer
     for (unsigned int j = 0; j < size; ++j) {
         if (myVector[j] == search) {
               total++;
          }
     }

     cout << endl << "The integer " << search << " appeared " << total << " times.";

}
Last edited on
that didnt work . . .

and it didnt display all the numbers it asked for a certain number.

i want it to show all the numbers and how many times it was used.



Say i enter 3,5,7,3 [enter]
0 seen 0 times
1 seen 0 times
2 seen 0 times
3 seen 2 times
4 seen 0 times
5 seen 1 times
6 seen 0 times
7 seen 1 times



something like that display up to the highest number, and start from 0
I retract what I said about vectors. Assuming you just want them entering numbers between 0 and 57, you can still use an array. I'm not sure why you made your initial array size 5 though, as you're not only requesting more than 5 numbers but also looking to keep track of 58 numbers.

Here's what I would do - I think it's a lot more simple than what the others have suggested ( you can also add in code to pass by the numbers that were not found at all if you wanted, but that's up to you):

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
#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
  int seen[58]={0};
  int x;

  printf("Enter ten integers between 0 and 57:\n");

    for (int i=0; i<10; i++)                // Not sure why you had this at 5
    {
        cout << "Number " << i+1 << ": ";   // Ask for the #
        cin >> x;
        if(x >= 0 && x <= 57)               // If it's the value you want...
            seen[x]++;                      // increment the corresponding memory location of your array by 1
        else                                // If the value isn't correct...
        {
            cout << "Please enter a valid number.\n";
            cin.clear();                    // clear the cache
            cin.ignore(1000,'\n');          // ignore up to 1000 characters or a new line
            i--;                            // decrement i by 1 to ask for the number over again
        }
    }
  //below is ok and right
    for (int i=0; i<=57; i++)
    {
        cout << i << " seen " << seen[i] << " times.\n";
    }

return 0;
}


I switched most of the streams to cin/cout since I hate prinf. :P This is all assuming you don't need to keep track of the numbers entered or anything. You can sift through the array and pull them out, but it's kind of a pain.

Hope that helps!
Last edited on
Istead of defining an array of 57 elements for only 10 entered numbers it would be simpler to define std::map<int, int>
Last edited on
no i want to be able to enter any number of integers. not only 10. that was just before i edited it.


but Kazekan is awesome.
Vlad, the array of 57 is being used as a counter. Also, we need to keep suggestions within a reasonable limit of what he's already learned in class.

And Zion, glad I could help. :)

Here's a very simple way to fix your issue though!

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
#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
    int seen[58]={0};
    int x, i=0;                     // Declare i now and set to 0.
    const int SENTINAL = 999;       // Set a sentinal value. If you haven't learned about these yet, just use a plain number in the while loop
    printf("Enter ten integers between 0 and 57:\n");

    while(x!=SENTINAL)                          // While x is not equal to the sentinal value...
    {
        cout << "Number " << i+1 << ": ";       // Ask for the #
        cin >> x;
        if(x==SENTINAL)                         // If x is the sentinal value, start the loop over again so that it stops the while loop.
            continue;                           // We could just use "break;" instead, but that's generally considered bad form.
        else if(x >= 0 && x <= 57)              // Otherwise, if it's the value you want...
        {
            i++;                                // increment i by 1...
            seen[x]++;                          // and increment the corresponding memory location of your array by 1
        }
        else                                    // If the value isn't correct...
        {
            cout << "Please enter a valid number.\n";
            cin.clear();                        // clear the cache
            cin.ignore(1000,'\n');              // ignore up to 1000 characters or a new line
        }
    }
  //below is ok and right
    for (i=0; i<=57; i++)
    {
        cout << i << " seen " << seen[i] << " times.\n";
    }

return 0;
}


Now it'll go on indefinitely, as long as they don't type 999. :D

Happy coding!
it works perfectly but when i enter something other than a number it start counting forever. speeding through numbers.


But your awesome does exacly what i want :) u dabes
You'll just need to add a statement in before the first if statement to check for invalid input. Depending on where you are in the learning process (and if this is for class or not), you can do it a number of ways. Basically though, you can add something simple like this in:



1
2
3
4
5
6
if(cin.fail())                              // If the input type does not match the variable type...
{
        cin.clear();                      // Reset the fail bit
        cin.ignore(1000,'\n');      // and ignore 1000 characters or up to a  new line, whichever comes first
        continue;                        // and continue the while loop to ask for another number.
}


Hope that helps
Topic archived. No new replies allowed.