How can i get this to display all information

November 27, 2010 at 16:02:11
Specs: Windows 7
int index=0;
char goAgain;
int counterForUsers=0;//counter for how many users were entered
int numberOfDownloadSum;//totals the number of downloads from each uer altogether
int userID[1000];
int numberOfDownloads[1000];

numberOfDownloadSum=0;
goAgain='y';

while(goAgain=='y'||goAgain=='Y')
{
cout<<" \n\t\t\tEnter your User ID number : ";
cin>>userID[index];
cout<<" \n\t\t\tEnter the number of Downloads for User: " ;
cin>>numberOfDownloads[index];

counterForUsers++;
numberOfDownloadSum=numberOfDownloads[index]+numberOfDownloadSum;

cout<<" \n\t\t\tDo you want to add another user?";
cin>>goAgain;
}

cout<<"\n\n\t\t\tUser ID: "<<" "<<" Number Of Downloads: "<<endl;
cout<<"\t\t\t---------------------------------"<<endl;

cout<<"\t\t\t"<<userID[index]<<"\t\t\t"<<numberOfDownloads[index]<<endl;
cout<<"\n\t\t\tNumber of users: "<<counterForUsers<<endl;
cout<<" \t\t\tTotal Number of downloads: "<<numberOfDownloadSum<<endl;
}

return 0;
}


See More: How can i get this to display all information

Report •

#1
November 27, 2010 at 16:39:57
where do you increment index? perhaps I overlooked it. counterForUsers can be used if it serves the same purpose since you're incrementing that...

while(...) {
userID[index] = some val;
numberOfDownloads[index] = some val;
++index;
}

note: you're risking overflowing your arrays in your control loop - if someone enters the data for 1001 records, both userID and numberOfDownloads will be overflowed; short circuit the loop test to include index being less than 1000 and goAain being 'y'.

to show all of your records, use a loop; for example if index denotes the total number of records.

for(int i=0; i < index; ++i) {
// output the results
}


Report •

#2
November 27, 2010 at 17:05:24
i have to create two arrays to use in parallel: one for user ids and the other for number of downloads. they should allow a large number of users maximum of 1000.
i shouldnt have to enter all 1000 ask the user if they want to add anthor

Report •

#3
November 27, 2010 at 17:18:59
*I also have to make it search for an id....now it is displaying the last input data twice so what do i do now


int main()
{
int i;
int index=0;
char goAgain;
int counterForUsers=0;//counter for how many users were entered
int numberOfDownloadSum;//totals the number of downloads from each uer altogether
int userId[1000];
int numberOfDownloads[1000];
bool found;
int foundUserId;
int searchId;
double serviceFeeAmount;

numberOfDownloadSum=0;
goAgain='y';


while(goAgain=='y'||goAgain=='Y')
{
++index;

cout<<" \n\t\t\tEnter your User ID number : ";
cin>>userId[index];
cout<<" \n\t\t\tEnter the number of Downloads for User: " ;
cin>>numberOfDownloads[index];

counterForUsers++;
numberOfDownloadSum=numberOfDownloads[index]+numberOfDownloadSum;

cout<<" \n\t\t\tDo you want to add another user?";
cin>>goAgain;
}

cout<<"\n\n\t\t\tUser ID: "<<" "<<" Number Of Downloads: "<<endl;
cout<<"\t\t\t---------------------------------"<<endl;

for(int i=0;i<index; ++i)
{
cout<<"\t\t\t"<<userId[index]<<"\t\t\t"<<numberOfDownloads[index]<<endl;
}
cout<<"\n\t\t\tNumber of users: "<<counterForUsers<<endl;
cout<<" \t\t\tTotal Number of downloads: "<<numberOfDownloadSum<<endl;

found=false;
do
{
if(userId[index]==searchId)
{
found=true;
foundUserId=index;
}

if(found)
{
cout<<"\t\t\nThe User Id: "<<searchId<<endl;

serviceFeeAmount=numberOfDownloads[foundUserId]*.99;
cout<<" \t\t\nNumber of Downloads: "<<numberOfDownloads[foundUserId]
<<" \t\t\nService Fee: "<<serviceFeeAmount<<endl;
cout<<" \t\t\nSearch for another user?";
cin>>goAgain;
}
else
{
cout<<" \n\t\t\tThis user has not registered for the service!!!!!"<<endl;
}
}while (goAgain=='y'||goAgain=='Y') ;

cout<<"\n\t\t\tGood-bye and thank you for using our report program. =)"<<endl;
return 0;
}


Report •

Related Solutions

#4
November 27, 2010 at 19:16:37
Your program needs to prepare for the unexpected. If you create an opportunity for a problem, the user is bound to find it sooner or later. :P

With your while loop, you're only asking a user if he or she wishes to add another record, but you don't check if the arrays are filled. If the user chooses yes forever and a day, then you've got problems. While 1000 items may seem like a lot, the user is in control of how many records are actually attempted, which isn't entirely what you'd want here.

while(index < 1000 && (doAgain == 'y' || doAgain == 'Y')) {
   assign user[index]
   assign numberofdownloads[index]
   ++index 
   ask for continuation
}

index - 1 is the upper bound of significant data or where the i < index comes from in the for loop.

As for the search, not sure what you're doing above. If you're outputting all records and searching for a specific record as well, then just use an if statement inside of the for loop. You're not wanting userId[index] == searchID but rather userId[i] == searchID.

HTH


Report •

#5
November 27, 2010 at 19:33:50
okay i am inputing two things into my program. but its still showing the last input that i put in and not all of the inputs

Report •

#6
November 27, 2010 at 22:17:22
hey every body
first thing : you have to write i instead of index in for loop
second thing you have to put i <=index.

for(int i=0; i <= index; ++i)
{
cout<<"\t\t\t"<< userId[ i ] <<"\t\t\t"<< numberOfDownloads[ i ] <<endl;
}

third thing : you have to add ++ to variables
else
{
cout<<" \n\t\t\tThis user has not registered for the service!!!!!"<<endl;
}
++index;
++foundUserId;
} while (index <1000 && (goAgain=='y'||goAgain=='Y') ) ;

and one more thing that i still dont know why it shows 0 0
at the first display data, maybe because tired. if you know tell me .
and i hope it will work because I am training ,not that much good in c++;


Report •

#7
November 28, 2010 at 06:12:06
Try this....................
.....................................................................................................................

#include <iostream.h>

int main()
{
int i;
int index=0;
char goAgain;
int counterForUsers=0;//counter for how many users were entered
int numberOfDownloadSum;//totals the number of downloads from each uer altogether
int userId[1000];
int numberOfDownloads[1000];
bool found;
int foundUserId;
int searchId;
double serviceFeeAmount;

numberOfDownloadSum=0;
goAgain='y';


while(goAgain=='y'||goAgain=='Y')
{
//++index; Arrays are 0 based. Don't you want to use first element. if you increment index here
// when we enter while loop, because of incrementing,index will be 1. if you use (one) 1 to access array element,
//you are accessing second ( 2 )element.
//........................................................................
cout<<" \n\t\t\tEnter your User ID number : ";
cin>>userId[index];
cout<<" \n\t\t\tEnter the number of Downloads for User: " ;
cin>>numberOfDownloads[index];
index++;
//........................................................................

counterForUsers++;
numberOfDownloadSum=numberOfDownloads[index]+numberOfDownloadSum;

cout<<" \n\t\t\tDo you want to add another user?";
cin>>goAgain;
}

cout<<"\n\n\t\t\tUser ID: "<<" "<<" Number Of Downloads: "<<endl;
cout<<"\t\t\t---------------------------------"<<endl;

for(int i=0;i<index; ++i)
{
cout<<"\t\t\t"<<userId[index]<<"\t\t\t"<<numberOfDownloads[index]<<endl;
}

cout<<"\n\t\t\tNumber of users: "<<counterForUsers<<endl;
cout<<" \t\t\tTotal Number of downloads: "<<numberOfDownloadSum<<endl;

/*found=false;
do
{
if(userId[index]==searchId)
{
found=true;
foundUserId=index;
}

//........................................................................
if(found)
{
cout<<"\t\t\nThe User Id: "<<searchId<<endl;

serviceFeeAmount=numberOfDownloads[foundUserId]*.99;
cout <<" \t\t\nNumber of Downloads: "<<numberOfDownloads[foundUserId]
<<" \t\t\nService Fee: "<<serviceFeeAmount<<endl;
cout<<" \t\t\nSearch for another user?";
cin>>goAgain;
}
else
{
cout<<" \n\t\t\tThis user has not registered for the service!!!!!"<<endl;
}
}while (goAgain=='y'||goAgain=='Y') ; */

//...........................................................................
found = false;
bool search = false;
do
{

cout << "Search Users..............";

//.................................................................
if ( search )
{
//..............................................................
cout << "\nEnter userId: ";
cin >> searchId;
//..............................................................
for ( int i = 0; i <= (index-1); i++ )
{
if ( userId[i] == searchId )
{
found = true;
foundUserId = i;
cout<<"\t\t\nThe User Id: "<<searchId<<endl;

serviceFeeAmount=numberOfDownloads[foundUserId]*.99;
cout <<" \t\t\nNumber of Downloads: "<<numberOfDownloads[foundUserId]
<<" \t\t\nService Fee: "<<serviceFeeAmount<<endl;
}
}
if (found == false) // you need to use a if statement here. guess why?
cout<<" \n\t\t\tThis user has not registered for the service!!!!!"<<endl;
}

cout << " \t\t\nDo you want to search for user? ";
cin >> goAgain;

if ( (goAgain == 'y') || (goAgain == 'Y') )
{
search = true;
}

}
while ( goAgain == 'y' || goAgain == 'Y' );
//............................................................................

cout<<"\n\t\t\tGood-bye and thank you for using our report program. =)"<<endl;
return 0;
}

.....................................................................................................................

.....................................
_Adnan_


Report •

#8
November 28, 2010 at 14:11:10
You've still got a couple of issues... mostly with accessing array elements. your arrays have 1000 elements, but are indexed 0-999.

if you want, you can use tolower/toupper, #include <cctype>

int index = 0, userID[1000] = { 0 }, numberOfDownloads[1000] = { 0 };
char doAgain = 'y';
... // other vars

while(index < 1000 && (tolower(doAgain) == 'y')) {
   cout << "Enter user ID: ";
   cin >> userID[index];
   cout << "Enter downloads: ";
   cin >> numberOfDownloads[index];
   ++index;

   cout << "add more users?";
   cin >> doAgain;
}

index is the number of total records and not a valid index position... you're "off by one." userID[index - 1] and numberOfDownloads[index - 1] are the last records in your data pairs. If you loop through the arrays, and use "index", then you'll always get the same result through each iteration of the loop.

searchID is not initialized, so its value can be anything. If it's a user entered value, then you need to read it into your program.

The do-while will break when found is false without giving the user a chance to search again... move the prompt outside of the if/else statement.

do {
   found = false;
   // prompt for user id to search
   // read in searchId
   // loop through records 
          for(int i=0; i < index; ++i) {
             if userId[i] == searchId  {
               // set found to true, get index (will be i), break out of for loop
             }
          }    
   // if found is true, show results else show the error message
   // prompt user to search again
} while(tolower(doAgain) != 'y');

HTH


Report •

Ask Question