here is my code. Here is a quick review of what i need to do: open a file of a list of numbers and get the first number and tally it for that number. Ex. number is 4563 it tallys 1 for number 4. And 2983 it tallys 1 for number 2. and loops through whole list and tallys numbers 1 - 9 and records them in the output. This outputs what i say but it outputs 0's for 1-9 instead of how many times it found a number that started with 1 and etc through 9.
Code:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int getFirstDigit(int num);
int main ()
{
ifstream inFile;
int number;
int num1=0, num2=0, num3=0, num4=0, num5=0, num6=0, num7=0, num8=0, num9=0;
string num11;
int totals[10]={0,0,0,0,0,0,0,0,0,0};
cout << "Enter 1 for librarybooks or Enter 2 for livejournal : ";
cin >> number;
if ( number == 1 )
{
inFile.open("librarybooks-1.txt");
switch(num11[0])
{
case '0' : totals[0]++;
break;
case '1' : totals[1]++;
break;
case'2' : totals[2]++;
break;
case'3' : totals[3]++;
break;
case'4' : totals[4]++;
break;
case'5' : totals[5]++;
break;
case'6' : totals[6]++;
break;
case'7' : totals[7]++;
break;
case'8' : totals[8]++;
break;
case'9' : totals[9]++;
break;
}
for (int i=1; i < 10; ++i)
{
cout << "Number of " << i << "s : " << totals[i] << endl;
}
}
else if ( number == 2 )
{
inFile.open("livejournal-1.txt");
switch(num11[0])
{
case '0' : totals[0]++;
break;
case '1' : totals[1]++;
break;
case'2' : totals[2]++;
break;
case'3' : totals[3]++;
break;
case'4' : totals[4]++;
break;
case'5' : totals[5]++;
break;
case'6' : totals[6]++;
break;
case'7' : totals[7]++;
break;
case'8' : totals[8]++;
break;
case'9' : totals[9]++;
break;
}
for (int i=1; i < 10; ++i)
{
cout << "Number of " << i << "s : " << totals[i] << endl;
}
}
else
cout << "\n" << "Invalid number" << "\n" << endl;
return 0;
}