Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So im making a linked list and in this list I'm trying to make a generate statistics function the code down below will show what I've done each time i go to generate the stats it just all comes back as 0 instead of 20% 50% etc would anyone have clue as to how i can fix this just the generatestatitics and serviceRating are being used to make this statics function work

What I have tried:

#include<stdio.h> 
#include<stdlib.h> 

#include<string.h> 
#include <list>



typedef struct NodeT_

{

	char name[30], lastName[20], accountNum[10], email[30];
	char addy[10];
	char id[30], service[20];
	double balance;
	float average;
	int paceOfService;

	struct NodeT_* NEXT;

}node;

void addElementAttheEnd(node* top);
void addElementAttheStart(node** top);
void generateStatistics(node* top);
int serviceRating(node* top)

node* HEAD = NULL;

int ticket = 0;



//global variables

int Excellent = 0;
int veryGood = 0;
int satisfactory = 0;
int Dissapointing = 0;
int Bad = 0;

int main()

{
	struct customer;
	node* HEAD = NULL;

	node* temp{};

	int option, data;
	int sizeDatabase = 30;
	int searchNum;
	int result;
	int foundLocation;
	char searchTerm[20];
	char username[20], password[20];

	FILE* inp;
	node fileValue{};

	printf("User name: ");
	gets_s(username);
	printf("Password: ");
	gets_s(password);



	if (strcmp(username, "Akeem") == 0)
	{
		if (strcmp(password, "Login100") == 0)
		{
			printf("Login succesful\n");
		}
		else
		{
			printf("Invalid password\n");
			return 0;
		}
	}

	else
	{
		printf("Inavild name\n");
		return 0;
	}



	/** Repopulate the linked list based on the file **/

	inp = fopen("Report.txt", "r");

	if (inp == NULL)

		printf("Sorry the file could not be opened\n");

	else
	{

		while (!feof(inp))
		{

			result = fscanf(inp, "%s %s %s %s %lf %s %s\n", fileValue.accountNum, fileValue.name, fileValue.lastName, fileValue.addy, fileValue.balance, fileValue.email, fileValue.service);

			if (result == 7)
			{
				printf("%s %s %s %s %lf %s %s\n", fileValue.accountNum, fileValue.name, fileValue.lastName, fileValue.addy, fileValue.balance, fileValue.email, fileValue.service);
				//Add the fileValue data to the list 
			}

		}

		fclose(inp);

	}



	printf("1) Add customer(Note: Account Number must be unique).\n");
	printf("2) Display all account whose balance is less than €100 to screen \n");
	printf("3) Display account holder details \n");
	printf("4) Lodge to Account \n");
	printf("5) Withdraw from Account \n");
	printf("6) Delete Account \n");
	printf("7) Generate statistics \n");
	printf("8) Print all account holders into a report file \n");
	scanf("%d", &option);



	while (option != -1)
	{

		if (option == 1)//This function will add a new account
		{
			generateStatistics(HEAD);
			
		}



		else if (option == 2)
		{
		displayToFile(HEAD);
		}

		printf("1) Add customer(Note: Account Number must be unique).\n");
		printf("2) Display all account whose balance is less than €100 to screen \n");
		printf("3) Display account holder details \n");
		printf("4) Lodge to Account \n");
		printf("5) Withdraw from Account \n");
		printf("6) Delete Account \n");
		printf("7) Generate statistics \n");
		printf("8) Print all account holders into a report file \n");
		scanf("%d", &option);
}


void addElementAttheEnd(node* top, char studentID[])

{

	node* temp;
	node* temp2;

	//Creating the new node 

	temp = (node*)malloc(sizeof(node));

	//Reading in the data  
	strcpy(temp->accountNum, studentID);
	printf("Please enter your first name\n");
	scanf("%s", &temp->name);
	printf("Please enter your surname\n");
	scanf("%s", &temp->lastName);
	printf("Please enter Address\n");
	scanf("%s", &temp->addy);
	printf("Enter your initial balance\n");
	scanf("%lff", &temp->balance);
	printf("Please enter your email\n");
	scanf("%s", &temp->email);

	temp->paceOfService = serviceRating(HEAD);

	temp->NEXT = NULL;



	//Finding the last node 

	temp2 = top;

	while (temp2->NEXT != NULL)

	{

		temp2 = temp2->NEXT;

	}



	temp2->NEXT = temp;

}



void addElementAttheStart(node** top, char studentID[])

{

	node* temp;



	//Creating the new node 

	temp = (node*)malloc(sizeof(node));
	//Reading in the data  
	strcpy(temp->accountNum, studentID);
	printf("Please enter your first name\n");
	scanf("%s", &temp->name);
	printf("Please enter your surname\n");
	scanf("%s", &temp->lastName);
	printf("Please enter Addres\n");
	scanf("%s", &temp->addy);
	printf("Enter your initial balance\n");
	scanf("%lff", &temp->balance);
	printf("Please enter your email\n");
	scanf("%s", &temp->email);

	temp->paceOfService = serviceRating(HEAD);

	temp->NEXT = NULL;

	temp->NEXT = *top; //*top currently has the address of the first node  

	//in the list which become the second node when temp is added infront of it 

	*top = temp;
}

void generateStatistics(node* top) // This function will display the stats for the service
{
	node* temp;


	int totalRating = 0;
	float excellentPercentage = 0;
	float veryGoodPercentage = 0;
	float satisfactoryPercentage = 0;
	float disappointingPercentage = 0;
	float badPercentage = 0;



	if (top == NULL)

	{

		printf("The list is empty\n");


	}

	else

	{

		temp = top;



		// look through the list to check if the Account is already in the list



		while (temp != NULL)

		{

			if (temp->paceOfService == '1')
			{

				Excellent++;



			}

			else if (temp->paceOfService == '2')

			{

				veryGood++;





			}

			else if (temp->paceOfService == '3')

			{

				satisfactory++;

			}

			else if (temp->paceOfService == '4')

			{

				Dissapointing++;



			}

			else if (temp->paceOfService == '5')

			{

				Bad++;

			}



			totalRating++;



			temp = temp->NEXT;





		}//end while



		excellentPercentage = (Excellent * 100) / totalRating;

		veryGoodPercentage = (veryGood * 100) / totalRating;

		satisfactoryPercentage = (satisfactory * 100) / totalRating;

		disappointingPercentage = (Dissapointing * 100) / totalRating;

		badPercentage = (Bad * 100) / totalRating;



		printf("%f of people who felt the service was excellent \n", excellentPercentage);

		printf("%f of people who felt the service was very good \n", veryGoodPercentage);

		printf("%f  of people who felt the service was satisfactory \n", satisfactoryPercentage);

		printf("%f  of people who felt the service was disappointing\n", disappointingPercentage);

		printf("%f  of people who felt the service was bad \n", badPercentage);

	}//end else

	//calculate statistics

}


int serviceRating(node* top) //This will count and add so we can generate the statistics
{
	int serviceRating;


	printf("How would you rate the pace of service in the bank?\n");

	printf("Press 1 for Excellent \n");

	printf("Press 2 for Very Good \n");

	printf("Press 3 for Satisfactory \n");

	printf("Press 4 for Disappointing \n");

	printf("Press 5 for Bad \n");

	scanf("%d", &serviceRating);

	return serviceRating;
}
Posted
Updated 5-Sep-21 9:02am

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on this line and run your app:
C
excellentPercentage = (Excellent * 100) / totalRating;

When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

And do yourself a favour: sort out your spacing! Multiple blank lines don't make code look bigger or better, they make it harder to read as you get far less of a function on the screen ...
 
Share this answer
 
Sounds like you have some bugs in your code. The best is to use the debugger and with some hard coded test data for which you know the results.

some tips:
- use less empty line
- use more structs and return values and pointers
- dont use global values
- store the statistic
 
Share this answer
 
Try calculating and displaying your percentages like this :
C++
excellentPercentage = ( (float) Excellent * 100.0f) / (float) totalRating;

printf("%5.1f %% of people felt the service was excellent \n", excellentPercentage);
 
Share this answer
 
Are you sure about
C++
if (temp->paceOfService == '1')

paceOfService being an integer, one can expect
C++
if (temp->paceOfService == 1)

Use the debugger to make sure the value of paceOfService.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900