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
#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.
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.
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.
#include <iostream>
#include <vector>
usingnamespace 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 (unsignedint j = 0; j < size; ++j) {
if (myVector[j] == search) {
total++;
}
}
cout << endl << "The integer " << search << " appeared " << total << " times.";
}
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):
#include <iostream>
#include <stdio.h>
usingnamespace 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.
#include <iostream>
#include <stdio.h>
usingnamespace std;
int main()
{
int seen[58]={0};
int x, i=0; // Declare i now and set to 0.
constint 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.
elseif(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
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.
}