How use array and add multiple enties in C++ - c++

My teacher wanted me to make a program based on An Employee which includes -
His Name
His Id
His Sallary
and we needed to make this using - Structures & Functions & Array as we are not that good in C++ so we just make simple programs . the code of the program i made is below , [ http://pastebin.com/9UFFJseN ]
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
#include<stdio.h>
struct employee
{
char empname[40];
int empid,empsalary;
}; employee e1;
void add_record(void);
void display_record(void);
void search_record(void);
void welcome();
void exito();
void main()
{
clrscr();
int choice,id;
cout<<"Welcome to Employee Info Menu by Rohit Jalan \n\n\n";
cout<<"1 -> Add Record\n";
cout<<"2 -> Display Record\n";
cout<<"3 -> Search Record\n";
cout<<"4 -> Exit";
cout<<"\n\t\t --> ";
cin>>choice;
switch(choice)
{
case 1: add_record();break;
case 2: display_record();break;
case 3: search_record();break;
case 4: exito();
}
getch();
}
void add_record()
{
clrscr();
cout<<"You have pressed 1\n\n\n";
cout<<"Please enter employee name : ";
gets(e1.empname);
cout<<"Please enter the employee id : ";
cin>>e1.empid;
cout<<"Please enter the employee salary : ";
cin>>e1.empsalary;
cout<<" \n \n \n \n";
main();
}
void display_record()
{clrscr();
cout<<"\nEmployee Name is : ";
puts(e1.empname);
cout<<"\nEmployee ID is : ";
cout<<e1.empid;
cout<<"\nEmployee salary is : ";
cout<<e1.empsalary;
getch();
main();
}
void search_record()
{int id;
clrscr();
cout<<"Please enter the id of the employee\n: ";
cin>>id;
if(id==e1.empid)
{
display_record();
}
else cout<<"\nRecord not found...";
getch();
main();
}
void exito()
{clrscr();
cout<<"\n\n\n\n\t\t\t Thank you. ";
cout<<"\n\n\n\n\n\n\t\t\t Program by \n\n\n";
cout<<"\t\t\t";
delay(500);cout<<"ROHIT";
delay(500);cout<<" JALAN";
cout<<"\n\t\t\t";
delay(500);cout<<"Roll No";
delay(500);cout<<" 11436";
cout<<"\n\t\t\t";
delay(500);cout<<"Class";
delay(500);cout<<" XI-D";
delay(500);cout<<"....";
delay(100);
exit(0);
}
And now my teacher is asking me what if she wishes to enter more then 1 record of data and display more then 1 , or you can say she wants to decide that how much she wants to enter and display , as i am unable to solve her problem i request you all guys to help me in a simple and easy manner as i`m newbie out here . Please don't delay in answer i have a practical examination on 27th Feb 2015 . if you are unable to see my coding -
http://pastebin.com/9UFFJseN#
Thanks In Advance .

In order to perform actions more than once, you need a loop.
unsigned int Display_Menu(void)
{
unsigned int selection = 0;
while (selection != 4)
{
static const char menu_text[] =
"\n"
"Welcome to Employee Info Menu by Rohit Jalan \n\n\n"
"1 -> Add Record\n"
"2 -> Display Record\n"
"3 -> Search Record\n"
"4 -> Exit\n"
"\t\t --> ";
cout.write(menu_text, sizeof(menu_text) - 1);
cin >> selection;
if ((selection >= 1) && (selection <= 4))
{
break;
}
cout << "\nInvalid selection, try again.\n";
}
return selection;
}
The above function displays a menu and if the User enters an invalid choice, it will print the menu again. After a valid choice is received, the function returns to the caller.
This is an example of doing something more than once.
So if your instructor wants to perform more than one action, how would you do it?
Edit 1:
In general a Menu consists of two pieces: 1) Displaying the selections and 2) Processing the choices. The above code handles the displaying of the selections.
// Forward declarations
void Add_Record(void);
void Display_Record(void);
void Search_Record(void);
void Process_Choices(void)
{
unsigned int choice = 0;
do
{
choice = Display_Menu();
switch (choice)
{
case 1: Add_Record(); break;
case 2: Display_Record(); break;
case 3: Search_Record(); break;
}
} while (choice != 4);
}
The above function displays the menu, calls functions according to the User's selection and repeats. It ends when the User enters the number 4.
Again, notice the loop construct that is used for performing actions more than once.

Related

How can I align the output program correctly? Once there was a 2 digit number, the alignment is being disoriented

It is a program that takes information then saves it to a marklist file and then outputs it again and then the output is not properly aligned to the rows above. How can I align the output program correctly? Once there was a 2 digit number, the alignment is being disoriented.Notice that the output is not properly aligned
#include<iostream>
//fstream is needed because we will use file systems.
#include<fstream>
using namespace std;
/*declaring fstream global variable used
to open, write, read and close files in this program.*/
fstream storage;
struct student_profile{
char FName[100];
char LName[100];
char reg_year[100];
char course[100];
char id[100];
int no_courses;
char course_Name[100];
char course_code[100];
int credit_hour;
int quiz_one;
int quiz_two;
int quiz_three;
int mid;
int assignment;
int lab;
int final_exam;
};
/* creating structure variable from student_profile.
the name this structure variable is student.
it is used to hold name, last name, id etc
*/
student_profile student;
/* function used to register student.
it accept student details then it write those data to file.
so student details will be saved in text file for later use.
*/
void function_to_register(){
cout<<" Write First Name of student: ";
cin>>student.FName;
cout<<" Write Last Name of student: ";
cin>>student.LName;
cout<<" Write Registration year: ";
cin>>student.reg_year;
cout<<" Write Course of student: ";
cin>>student.course;
cout<<" Write number of courses that student learns in this semester and register them one by one\n";
cout<<" Number of courses: ";
cin>> student.no_courses;
/* for loop used to register multiple courses for one student
based on the student's number of courses entered.
*/
for(int a=1; a<=student.no_courses; a++) {
cout<<" Write course Name: ";
cin>>student.course_Name;
cout<<" Enter grade for quiz one: ";
cin>>student.quiz_one;
cout<<" Enter grade for quiz two: ";
cin>>student.quiz_two;
cout<<" Enter grade for quiz three: ";
cin>>student.quiz_three;
cout<<" Enter midterm grade: ";
cin>>student.mid;
cout<<" Enter assignment grade: ";
cin>>student.assignment;
cout<<" Enter lab grade: ";
cin>>student.lab;
cout<<" Enter final exam grade: ";
cin>>student.final_exam;
storage.open("marklist.txt",ios::app) ;
storage.write((char*)&student, sizeof(student));
//storage.write(student,sizeof(student));
storage.close();
}
}
/* function used to calculate grade of each course.
we call this function when the program is displaying grade of student.
*/
void grade_calculator(int totalMark) {
if(totalMark>=90 && totalMark<=100)
cout<<"A+";
else if(totalMark>=80 && totalMark<90)
cout<<"A";
else if(totalMark>=70 && totalMark<80)
cout<<"B";
else if(totalMark>=60 && totalMark<70)
cout<<"C+";
else if(totalMark>=50 && totalMark<60)
cout<<"D";
else if(totalMark>=0 && totalMark<50)
cout<<"F";
else
cout<<"NG";
}
/* function used to display registered students on the screen.
this function gets the registered student from that file
we used above to store student details.
*/
void seeRegistered() {
storage.open("marklist.txt",ios::app);
storage.close();
storage.open("marklist.txt",ios::in);
storage.read((char*)&student, sizeof(student));
while (storage.eof()==0) {
cout<<"\n\n\n";
cout<<" Full Name: "<<student.FName<<" "<<student.LName<<endl;
cout<<" Registration Year: "<<student.reg_year<<endl;
cout<<" Faculty: "<<student.course<<endl;
cout<<" *****************************************************************************\n";
cout<<" ******************************STUDENT MARK LIST******************************\n";
cout<<" *****************************************************************************\n";
cout<<" Subject Quiz1 Quiz2 Quiz3 Mid Assignment Final Total Grade\n";
cout<<" _____________________________________________________________________________\n";
/* for loop used to print all courses and their marks
for a single student.
*/
for(int t=1; t<=student.no_courses; t++) {
int totalMark;
totalMark= (student.quiz_one+student.quiz_two+student.quiz_three+student.assignment+student.mid+student.final_exam)/6;
cout<<" "<<student.course_Name<<""<<student.quiz_one<<" "<<student.quiz_two<<" "<<student.quiz_three<<" "<<student.mid<<" "<<student.assignment<<" "<<student.final_exam<<" "<<totalMark<<" ";
grade_calculator(totalMark);
cout<<endl;
storage.read((char*)&student,sizeof(student));
}
}
storage.close();
}
/* This is the main function which will be executed before all functions,
So in this program when the user select one option then by
the help switch statement the user redirects to appropriate function.
*/
int main() {
char select;
/* while loop that help us to excute the program all time.
this means the program will not exit without our interest.*/
while(1) {
cout<<endl; cout<<endl; cout<<endl;
cout<<endl;
cout<<" **********************************************\n";
cout<<" * STUDENT REGISTRATION AND MARK LIST SYSTEM *\n";
cout<<" **********************************************\n";
cout<<" *\n";
cout<<" Enter 1 To Register New student \n";
cout<<" Enter 2 To Show Registerd Students and Their Grade \n";
/*
you can improve this in which the displayed record is sorted (ascending/descending)
*/
cout<<" Enter 3 To Search for a student \n";
cout<<" Enter 4 To Edit a student record\n";
cout<<" Enter 5 To Delete a student record\n";
cout<<" Enter 0 To Exit \n";
cout<<" : ";
cin>>select;
switch (select) {
case '1':
function_to_register();
cout<<" Student Registered succsesfully \n";
break;
case '2':
seeRegistered();
break;
}
}
}
use std::cout<<setw(num_of_spaces)

Issues with what appears to be never ending loops when calling functions in switch statement (even if function contains no loop)

I am trying to make a really simple text menu of sorts using switch statements for a class assignment. The issue is when calling the various functions I've made in the different cases of the switch statement, the never seem to end.
Considering the same issue seems to be happening to all the functions in my switch statement I think it might have something to do with the switch statement itself and not the individual functions although I honestly don't know at this point.
Disclaimer: I know I have tons of other issues with my code, but im just trying to pick a big one for simplicity. I would appreciate advice on other parts of my code aswell. (Also its not done yet as you can probably see by the unfinished cases)
Example output:
==================
1. Admission's Office
2. Student
3. End Program
==================
Enter your choice: 1
Enter the password: 123
******************
1. Add a new student
2. Add new students from a file
3. Drop a student
4. View one students info
5. View all students info
******************
Enter your choice: 1
Enter the first name: First Name
Enter the last name: Last Name
Enter the gender: m
Enter the first name: It should only ask me once
Enter the last name: Why is this looping?
Enter the gender: ????
Enter the first name:
Enter the last name: ???????
Enter the gender: a
Enter the first name: ^C
Code
//main.cpp
int main() {
Student stuAr[SIZE];
int choice, password, userInput;
int numStu = 0;
bool valid;
do {
userMenu();
cin>>choice;
cout<<endl;
switch(choice){
case 1:
cout<<"Enter the password: ";
cin>>password;
cout<<endl;
valid = checkPass(password);
if (valid) {
adminMenu();
cin>>choice;
cout<<endl;
do {
switch(choice) {
case 1:
addStu(stuAr, numStu);
break;
case 2:
addStuFile(stuAr, numStu);
break;
case 3:
cout<<"Enter the student ID: ";
cin>>userInput;
cout<<endl;
dropStu(stuAr, numStu, userInput);
break;
case 4:
cout<<"Enter the student ID: ";
cin>>userInput;
cout<<endl;
viewStu(stuAr, numStu, userInput);
break;
case 5:
viewAllStu(stuAr, numStu);
break;
}
}while(choice != 6);
}
else {
cout<<"The password is wrong."<<endl;
}
break;
case 2:
break;
}
}while(choice != 3);
return 0;
//still main.cpp
//addStu function for case 1 of the nested switch statement
void addStu(Student stuAr[], int& numStu) {
cin.ignore();
cout<<"Enter the first name: ";
stuAr[numStu].setFN();
cout<<endl;
//cin.ignore();
cout<<"Enter the last name: ";
stuAr[numStu].setLN();
cout<<endl;
cout<<"Enter the gender: ";
stuAr[numStu].setGender();
cout<<endl;
numStu++;
return;
}
You are never rereading choice within your inner loop.
if (valid) {
adminMenu();
cin>>choice; // only done once, therefore do loop never terminates
cout<<endl;
do {
switch(choice) {
case 1:
addStu(stuAr, numStu);
break;
// ...
}
// after switch-case, add:
cin >> choice;
cout << endl;
You never change choice in the inner do..while loop, and therefore the condition choice != 6 will always be true, unless you enter it in the first time.

unable to write to/read from file

The code I wrote seems to work when i write data in the file but when i look into the record.dat file after writing into it, it shows nothing. The segment with "Main Menu" gets repeated from the switch() every time i try to read from the file. I had to write the open commands with the access modes separately inside each case otherwise it wouldn't even open the file.
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <conio.h>
using namespace std;
class employee
{
private:
int empcode;
char empname[30];
char empdesig[15];
float empsalary;
public:
void add_rec()
{
cout<<"\nEmployee Code : ";
cin>>empcode;
cout<<"\nEmployee Name : ";
cin.ignore();
cin.getline(empname,30);
cout<<"\nEmployee Designation : ";
cin.getline(empdesig,15);
cout<<"\nEmployee Salary : ";
cin>>empsalary;
}
void read_rec()
{
cout<<"\nEmployee Code : "<<empcode;
cout<<"\nEmployee Name : "<<empname;
cout<<"\nEmployee Designation : "<<empdesig;
cout<<"\nEmployee Salary : "<<empsalary;
}
};
int main()
{
employee emp;
fstream rfile;
int ch,rec_no=0,pos=0;
char ans='y',opt;
do
{
cout<<"\n";
cout<<"\t\t\t MAIN MENU ";
cout<<"\n1. Add Record ";
cout<<"\n2. Read Record ";
cout<<"\n3. Modify Record ";
cout<<"\n4. Exit "<<endl;
cout<<"\nSelect an option : ";
cin>>ch;
switch (ch)
{
case 1:
{
rfile.open("record.dat", ios::out);
char opt='y';
cout<<"\t\t\tEmployee Data Entry "<<endl;
do
{
emp.add_rec();
rfile.write((char*)&emp,sizeof(emp));
cout<<"\nEnter another record ? {Y/N} ";
cin>>opt;
} while(opt=='y'||opt=='Y');
}
break;
case 2:
{
rfile.open("record.dat", ios::in);
cout<<"\t\t\tEmployee Data Display "<<endl;
rfile.read((char*)&emp,sizeof(emp));
while(rfile)
{
emp.read_rec();
rfile.read((char*)&emp,sizeof(emp));
}
}
break;
case 3:
{
rfile.open("record.dat", ios::out);
cout<<"\t\t\tEmployee Data Modify "<<endl;
cout<<"\nEnter the record no. to modify : ";
cin>>rec_no;
pos=(rec_no-1)*sizeof(emp);
rfile.seekg(pos,ios::beg);
rfile.read((char*)&emp,sizeof(emp));
cout<<"\nModify this record ? {Y/N} "<<endl;
cin>>opt;
if(opt=='y'||opt=='Y')
{
cout<<"\n";
cout<<"\t\t\tEnter New Data "<<endl;
emp.add_rec();
rfile.write((char*)&emp,sizeof(emp));
cout<<"\nRecord Modified"<<endl;
cout<<"\nPress any key to continue...";
getch();
}
}
break;
case 4:
break;
default:
cout<<"\nPlease select a valid option!";
break;
}
} while(ch!=4);
rfile.close();
return(0);
}
Each time you open a file for writing under a case statement, you also need to close the file. When the file is closed, the double buffered values are then written to the physical file.
Under case 1, add a file close after the while statement:
} while(opt=='y'||opt=='Y');
rfile.close();
Make similar updates to case 2 and 3, and both case 1 and 2 will now work. Case 3 has additional issues. For example, when opening the file to modify, you will want to use the "app" bit mask so you do not delete existing contents. See the following for more information:
https://en.cppreference.com/w/cpp/io/basic_fstream/open

How to fix an "expected primary-expression" error

I'm working on a project in our programming class which requires me to make an inventory system. There is an error in the code in line 243 that gives weird errors when I compile it.
Line 243 beneath in the code is
while(inventory>>rec.prodtype>>rec.menutype>>rec.prodnum
#include<iostream>
#include<windows.h>
#include<string>
#include<fstream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
void gotoXY(int x, int y);
void add();
void view();
void menu();
void types();
void input();
void stock();
void find_name();
void find_number();
void find_type();
void find_mtype();
void all();
void output();
struct inventory{
string prodtype,menutype;
string prodnum,prodname;
int quantity;
float prodprice;
}rec;
int main()
{
int x=1,y=1; //for gotoXY
char l; //choice to determine add/view
system("cls");
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\t INVENTORY SYSTEM\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\n\t\t ---- [1] Add new product ----"
<<"\n\t\t ---- [2] Add stocks to existing products ----"
<<"\n\t\t ---- [3] View Products ----"
<<"\n\t\t ---- [4] Exit Program ----"
<<"\n\n\t\t\t\t Enter choice : ";
back:
l=getch();
if(l=='1')
add();
else if(l=='2')
stock();
else if(l=='3')
view();
else if(l=='4')
return 0;
else
goto back;
}
void stock_intro()
{
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\t ADDING OF STOCKS\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n\n";
}
void stock()
{
string num; //for inputting prodnum...
int x; //for adding stocks
string prodtype,menutype;
string prodnum,prodname;
int quantity;
float prodprice;
ifstream inventory("inventory.txt");
system("cls");
stock_intro();
cout<<"\t\t\t Enter Product Number to add stock:\n\n";
cout<<"\t\t\t ";
cin>>num;
while(inventory>>prodtype>>menutype
>>prodnum>>prodname>>quantity
>>prodprice){
system("cls");
cout<<"How many stocks would you like to add?: ";
cin>>x;
rec.quantity=rec.quantity+x;
}
}
void types()
{
ofstream inventory("inventory.txt", ios::app);
char c;
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\tFOOD AND BEVERAGES\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n";
cout<<"\t\t\t\t [A] SNACKS\n\t\t\t\t [B] CANDIES\n\t\t\t\t [C] BISCUITS\n\t\t\t\t [D] CHOCOLATES\n\t\t\t\t [E] CANNED GOODS\n\t\t\t\t [F] CONDIMENTS\n\t\t\t\t [G] INSTANT NOODLES\n\t\t\t\t [H] BEVERAGE / DRINKS\n";
cout<<"\n\t\t\t\t Enter Choice: ";
back:
c=getch();
c=toupper(c);
if(c=='A')
rec.menutype="SNACKS";
else if(c=='B')
rec.menutype="CANDIES";
else if(c=='C')
rec.menutype="BISCUITS";
else if(c=='D')
rec.menutype="CHOCOLATES";
else if(c=='E')
rec.menutype="CANNED GOODS";
else if(c=='F')
rec.menutype="CONDIMENTS";
else if(c=='G')
rec.menutype="INSTANT NOODLES";
else if(c=='H')
rec.menutype="BEVERAGE / DRINKS";
else
goto back;
inventory<<rec.menutype;
}
void input()
{
system("cls");
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\tADDING OF PRODUCTS\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n\n";
ofstream inventory("inventory.txt", ios::app);
cout<<"\t\t\tEnter Product number: ";
cin>>rec.prodnum;
cout<<"\t\t\tEnter Product name: ";
cin>>rec.prodname;
cout<<"\t\t\tEnter Product price: ";
cin>>rec.prodprice;
cout<<"\t\t\tEnter Quantity: ";
cin>>rec.quantity;
inventory<<endl
<<rec.prodnum<<endl
<<rec.prodname<<endl
<<rec.prodprice<<endl
<<rec.quantity<<endl<<endl;
}
void add()
{
char c,d; //for choosing food or hygene
char e; //for choosing if add another of not
back2:
system("cls");
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\t ADD PRODUCTS\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
ofstream inventory ("inventory.txt", ios::app);
cout<<"\n\n\t\t\t\t[A] FOOD and BEVERAGES\n\t\t\t\t[B] HYGIENE";
cout<<"\n\n\t\t\t\tEnter Choice: ";
back1:
c=getch();
d=toupper(c);
if(d=='A')
rec.prodtype="Food & Beverages";
else if(d=='B')
rec.prodtype="Hygiene";
else
goto back1;
system("cls");
inventory<<rec.prodtype<<endl;
types();
input();
cout<<"\n\t\t\tEnter another product? [Y / N]: ";
back3:
e=getch();
e=toupper(e);
if(e=='Y')
goto back2;
else if(e=='N')
main();
else
goto back3;
}
void output()
{
cout<<"Product Type: "
<<rec.prodtype<<endl;
cout<<"Menu Type : "
<<rec.menutype<<endl;
cout<<" Product Number: "
<<rec.prodnum<<endl;
cout<<" Product Name: "
<<rec.prodname<<endl;
cout<<" Product Price: "
<<rec.prodprice<<endl;
cout<<" Stocks Available: "
<<rec.quantity<<endl;
}
void view_intro()
{
system("cls");
cout<<"\n\nÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ";
cout<<"\t\t\t\tVIEWING OF INVENTORY\n";
cout<<"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ\n";
}
void view()
{
char ch;
system("cls");
view_intro();
cout<<"\n\t\t ---- [1] Find by Product Name ----"
<<"\n\t\t ---- [2] Find by Product Number ----"
<<"\n\t\t ---- [3] Find by Product Type ----"
<<"\n\t\t ---- [4] Find by Menu Type ----"
<<"\n\t\t ---- [5] Display all Records ----"
<<"\n\n\t\t\t\t Enter choice : ";
back:
ch=getch();
ch=getch();
switch(ch)
{
case '1': find_name();break;
case '2': find_number();break;
case '3': find_type();break;
case '4': find_mtype();break;
case '5': all();break;
default: goto back;
}
}
void find_name()
{
string name;
int v=0;
system("cls");
view_intro();
ifstream student("record.txt");
cout<<"\n Enter Product Name [incorrect capitalization is invalid]: ";
cin>>name;
system("cls");
view_intro();
while(inventory>>rec.prodtype>>rec.menutype>>rec.prodnum
>>rec.prodname>>rec.prodprice>>rec.quantity)
{
back:
while(name==rec.prodname)
{
output();
break;
}
v++;
if(v<1)
{
goto back;
}
}
system("pause");
main();
}
void gotoXY(int x, int y)
{
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console,CursorPosition);
}
In find_name() function:
while(inventory>>rec.prodtype>>rec.menutype>>rec.prodnum
>>rec.prodname>>rec.prodprice>>rec.quantity)
You dont declare what inventory is in this function. Did you mean to open a file in a variable called inventory like you did in the other functions?
The reason the compiler is giving weird errors is you declared a type called inventory and so not seeing a local variable called inventory it assumes you're using the type which makes no sense in the context of the above line and the compiler gets confused and gives a fairly obscure error.

Program on cricket scoreboard

Why is the following code not giving results and how to get results?
Whenever I run the code, it first asks for the names of the players of two teams playing the match, then it shows the menu from which if we select any one of the option it again asks for the batsman name which is not according to the program designed. My research on the code and the problem is that I think buffer memory is full but I don't know how to free it, any help would be beneficial. Thank you
#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
class scorecard{
char batname[11][20];
int runscored[11];
char situation[11][10];
char mode[11][15];
char bowlername[11][20];
float oversplayed[11];
int maiden[11];
int runsgiven[11];
int wicketstaken[11];
public:
void updatebatsman(void);
void updatebowler(void);
void displaybat(void);
void displaybowl(void);
void menu(void);
scorecard()
{for(int n=0;n<12;n++)
{
runscored[n]={0};
oversplayed[n]={0};
maiden[n]={0};
runsgiven[n]={0};
wicketstaken[n]={0};
}
}
};
int main()
{
int jb=0;
scorecard s1;
int kb;
s1.menu();
do
{
cout<< "Enter the option"<<endl;
cout<<"(1) Display batting score"<<endl<<"(2) Display Bowling score"<<endl<<"(3) Update batting score"<<endl;
cout<<"(4) Update Bowling score"<<endl;
cin >>kb;
switch(kb)
{
case 1 : s1.displaybat();
break;
case 2 :s1.displaybowl();break;
case 3:s1.updatebatsman();break;
case 4:s1.updatebowler();break;
default:cout<<"Wrong choice";
}
}while (jb<1);
}
void scorecard::updatebowler(void)
{char bowlname[20];
int str,k,option,overnumbers,maidenumb,uprun,upwicket;
cout<<"Enter Bowler name:";
cin.getline(bowlname,20);
for( k=0;k<11;k++)
{str= strcmp(bowlername[k],bowlname);
if (str== 0)
{
cout<<"Menu for Bowler information update "<<endl;
cout<<"(1) Update Number of overs"<<endl<<"(2) Update maiden overs"<<endl<<"(3) Update runs given"<<endl;
cout<<"(4) Update wickets taken"<<endl;
cin >> option;
switch(option)
{
case 1:{cout<<"Enter Numbers of overs to be updated:";
cin >>overnumbers;
cout<<endl;
oversplayed[k]+=overnumbers;
break;
}
case 2:{cout <<"Enter the number of maiden overs to be updated:";
cin>>maidenumb;
cout<<endl;
maiden[k]+=maidenumb;
break;
}
case 3:{cout <<"Enter the number of runs to be added:";
cin>>uprun;
cout<<endl;
runsgiven[k]+=uprun;
break;
}
case 4: {cout<<"Enter number of wickets to be updated:";
cin >>upwicket;
cout<<endl;
wicketstaken[k]+=upwicket;
}
default:cout<<"wroung choice";
}
break;
}
}
if (str!=0)
cout <<"You entered wrong player."<<endl;
}
void scorecard::updatebatsman(void)
{char batsmaname[20];
int str,k;
cout<<"Enter Batsman name:";
cin.getline(batsmaname,20);
for( k=0;k<11;k++)
{str= strcmp(batname[k],batsmaname);
if (str== 0)
{
cout<<"enter runs scored:";
cin>>runscored[k];
cout<<endl<<"enter weather out or not out:";
cin>>situation[k];
cout<<endl<<"enter mode(if batsman out) by which batsman was out:";
cin>>mode[k];
break;
}
}
if (str!=0)
cout <<"You entered wrong player."<<endl;
}
void scorecard::displaybat(void)
{
cout << "Batsman name"<<'t'<<"Runs scored"<<'t'<<"situation"<<'t'<<"mode"<<endl;
for(int j=0;j++;j<12)
{
cout<<batname[j]<<'t'<<runscored[j]<<'t'<<situation[j]<<'t'<<mode[j]<<endl;
}
}
void scorecard::displaybowl(void)
{
cout << "Bowler name"<<'t'<<"overs played"<<'t'<<"maiden overs"<<'t'<<"wicket taken"<<'t'<<"Runs given"<<endl;
cout<<endl;
for(int j=0;j++;j<12)
{
cout<<bowlername[j]<<'t'<<oversplayed[j]<<'t'<<maiden[j]<<'t'<<wicketstaken[j]<<'t'<<runsgiven[j]<<endl;
}
}
void scorecard::menu(void)
{
cout<<"Enter the name of players of batting team"<<endl;
for (int k=0;k<11;k++)
{
cout <<"Enter name of player "<<k+1<<":";
cin>>batname[k];
}
cout <<"Enter the name of players of bowling team"<<endl;
for (int n=0;n<11;n++)
{
cout <<"Enter name of player "<<n+1<<":";
cin>>bowlername[n];
}
}
This is very wrong:
for(int j=0;j++;j<12)
It should be:
for(int j=0; j < 11; j++)
You are also missing a break in your case 4 statement for the options:
case 4: {cout<<"Enter number of wickets to be updated:";
cin >>upwicket;
cout<<endl;
wicketstaken[k]+=upwicket;
break;
}
default:cout<<"wroung choice";
Without the break you will see also the output wrong choice when the user selects option 4.