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.
Related
I am trying to create a program to represent Graph using Adjacency List. In that I have created a menu-driven program and implemented non-class functions to create the graph, create edges between the vertices and to display the graph.
The main question is to represent a map of area in the form of graph and use single characters (like 'a','b' or 'c' etc) to represent the cities. Following is the code:
#include<iostream>
using namespace std;
class Graph
{
public:
char city;
int distance;
Graph *next;
};
Graph **graph=new Graph*[20];
int size;
void display()
{
for(int i=0;i<size;i++)
{
Graph *temp=graph[i];
while(temp!=NULL)
{
cout<<temp->city<<"--"<<temp->distance<<"--";
if(temp->next==NULL)
cout<<"NULL"<<endl;
}
}
}
void edges()
{
for(int i=0;i<size;i++)
{
for(int j=0;j<i;j++)
{
if(i==j)
continue;
Graph *temp=graph[i],*node=new Graph;
char input;
cout<<"\nIs there connection between " << graph[i]->city << " and " << graph[j]->city << "? Enter y or n: ";
cin>>input;
if(input=='y' || input=='Y')
{
cout<<"\nEnter the distance between them:";
cin>>node->distance;
temp->next=node;
temp=temp->next;
temp->next=NULL;
}
}
cout<<endl;
}
cout<<"Edges Created Successfully\n";
}
void create()
{
cout<<"Enter the number of cities:";
cin>>size;
cout<<"Enter "<<size<<" cities\n";
for(int i=0;i<size;i++)
{
cout<<"City number "<<i+1<<":";
cin>>graph[i]->city;
cout<<endl;
}
}
int main()
{
while(true)
{
int choice;
cout<<"\n1.Create a graph\n";
cout<<"2.Create the connections between the vertices\n";
cout<<"3.Display the graph\n";
cout<<"4.Exit\n";
cout<<"Enter your choice:";
cin>>choice;
switch(choice)
{
case 1:
create();
break;
case 2:
edges();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
cout<<"Invalid Choice"<<endl;
break;
}
}
}
The error I am getting is : Whenever I run the program, the program terminates unexpectedly after taking 3rd city as input. It does not allow me to take further inputs as well as to perform other operations.
Please help me as this error is quite new to me. I think the cause of the error might be due to dynamically created Graph pointers array. But it is just a guess and I do not know the real cause of this problem.
A basic student database using classes, I accept
name,
roll number, and
sgpa(cgpa equivalent but out of 10).
I used while loop, and switch case,but if the code for sgpa validation, and code for displaying all students with same sgpa is removed, the program works neatly, but if not, the program goes into continuous loop, the while loop inside the main, as it accepts the options itself and keeps doing it.
The code fails when i accept the sgpa(while taking the student data) or call the displaySGPA fucntion(option 3)
int totalStudents=0;
class database{
float sgpa;
int roll;
string name;
public:
void getSGPA();
void getData(database []);
friend void displaySGPA(int,database []);
};
//Display Students with same SGPA
void displaySGPA(int temp,database students[]){
int i,sameSGPA=0;
for(i=0;i<totalStudents;i++){
if(students[i].sgpa==temp){
sameSGPA+=1;
}
}
if(sameSGPA>1){
cout<<"\nStudents with SGPA "<<temp<<"."<<endl;
for(i=0;i<totalStudents;i++){
if(students[i].sgpa==temp){
cout<<" "<<students[i].roll<<" "<<students[i].name;
}
}
}
else if(sameSGPA==1){
cout<<"Only one student with the SGPA , "<<temp<<". :"<<endl;
for(i=0;i<totalStudents;i++){
if(students[i].sgpa==temp){
cout<<" "<<students[i].roll<<" "<<students[i].name;
break;
}
}
}
else{
cout<<"No Student with given SGPA."<<endl;
}
}
void database :: getData(database students []){
cout<<"\nEnter Name : ";
cin>>name;
cout<<"Enter Roll. ";
cin>>roll;
getSGPA();
}
//SGPA validation
void database :: getSGPA(){
int x=1,temp;
while(x==1){
cout<<"Enter SGPA : ";
cin>>temp;
if(temp<=10){
sgpa=temp;
break;
}
else{
cout<<"Please enter a valid SGPA.";
}
}
}
//main Loop
int main() {
int x,temp;
database students[50];
while (x!=5){
cout<<"\n1.Enter a New Student.\n3.Display students with same SGPA.\n5.Exit.\n\nYour Choice : ";
cin>>x;
switch(x){
case 1:
students[totalStudents].getData(students);
totalStudents++;
break;
case 3:
cout<<"\nEnter the SGPA.";
cin>>temp;
displaySGPA(temp,students);
break;
case 5:
cout<<"Exiting the program.....";
break;
default:
cout<<"Please select a valid option."<<endl;
}
}
}
I found the bug, it was a type conversion bug, i was accepting a float value but passing it as a int,just worked, don't know why.But thanks for each comment and suggestion.
I am currently studying c++ but I fell behind a little bit, so I apologize if my question is obvious.
I have to create a program that asks for a student's name, GPA, Year of admission, and get a random 5 digit number generated for that person. The number of students will not exceed 42.
My program compiled (somehow) and I am able to get the error for invalid menu selection, however, whenever I give a valid selection (currently 1) nothing happens.
Maybe I am missing something, this is why I need help.
Here is my code.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//print all the menu options
void print_menu()
{
cout<<"\nRCNJ Registrar Menu:"<<"\n"
<<"\n"
<<"[1] Add a student"<<"\n"
<<"[2] Display all students"<<"\n"
<<"[3] Display by year"<<"\n"
<<"[4] Display statistics"<<"\n"
<<"[5] Quit"<<"\n";
}
//get and return the student's name
void get_name(string& student_name) //call student_name after that.
{
cout<<"Please enter the sudent's name: ";
cin >> student_name;
cout<<"\n";
}
//validate and return gpa
double get_gpa()
{
double student_gpa = 0;
cout<<"Please enter the GPA: ";
cin >>student_gpa;
cout<<"\n";
while (student_gpa > 4 || student_gpa < 0)
{
cout<<"Please enter a valid GPA for the student (0.00 - 4.00): ";
cin >> student_gpa;
cout<<"\n";
}
return student_gpa;
}
//validateand return year
int get_year()
{
int student_year = 0;
cout<<"Please enter the year: ";
cin >> student_year;
cout<<"\n";
while (student_year >2016 || student_year <1972)
{
cout<<"Please enter a valid year (min 1972, max 2016): ";
cin >> student_year;
cout<<"\n";
}
return student_year;
}
//generate the student's R#
int generate_number()
{
int r_number;
srand (time(NULL));
r_number = rand() % 89999 + 10000;
return r_number;
}
//save info. Include get_name, get_gpa, get_year
void input_new_student()
{
string student_name;
double student_gpa;
int student_year;
int r_number;
int s_name, s_gpa, s_year, r_num;
get_name(student_name);
get_gpa();
get_year();
generate_number();
}
//display all students in the proper format
void print_all()
{
}
//get a year as selection and print all students that are the same year
void print_by_year()
{
}
//display statistics based on entered students
void print_statistics()
{
}
//validate and return the menu option selected by the user.
//it should call print_menu defined earlier
int get_selection(int menu_choice)
{
menu_choice = 0;
cout<<"\n"
<<"Selection: ";
cin >> menu_choice;
cout<<"\n";
while (menu_choice > 5 || menu_choice< 1)
{
cout<<" Menu choice is invalid. Please re-enter (1 - 5): ";
cin>> menu_choice;
cout<<"\n";
}
return menu_choice;
}
int main()
{
string student_name;
double student_gpa;
int student_year;
int r_number;
int menu_choice;
int s_name=0;
int s_gpa=0;
int s_year=0;
int r_num=0;
string nameArray[42];
s_name++;
double gpaArray[42];
s_gpa++;
int yearArray[42];
s_year++;
int ramapoArray[42];
r_num++;
print_menu();
get_selection(menu_choice);
switch (menu_choice)
{
case 1:
input_new_student();
nameArray[s_name] = student_name;
gpaArray[s_gpa] = student_gpa;
yearArray[s_year] = student_year;
ramapoArray[r_num] = r_number;
break;
}
return 0;
}
I dont have permission to comment, hence adding it here.
In you main(),
get_selection(menu_choice);
switch (menu_choice)
You return menu_choice, but there is none to take the value, you end you using garbage value as it is uninitialized.
So two ways you can do it, either by passing the address/reference of menu_choice or by return value. try either of these it should work, though I have not gone through the rest of your program.
As suggested by others, try a debugger e.g. gdb?
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.
How Can I return all the structures when i choose 7.Logout after i insert a record from the Admin menu, and if i return to the admin menu, i'll see the record again.
For Example
User:Admin
Pass:Admin
then i choose 2.Insert Product after that Ill go choose 1.display , after that i 7.log out. now i want to login again as Admin but i want to see all the data again.please help this is our thesis project.
#include <iostream>
#include <string>
#include <iomanip>
#include <conio.h>
using namespace std;
struct product{
string pname,pid;
int qnty;
double price;
product *ptr;
};
//Function Declaration
void Menu(string username);
void InsertProduct();
void DeleteProduct();
void DisplayProduct();
void SearchProduct();
void PurchaseProduct();
//
product *head;
int main() //Account Login
{
int pw=0,n;
char pwc=' ';
string username,password;
cout<<"\n ==============================================================================="<<endl<<endl;
cout<<"\t\t\t LICA'S GROCERY STORE"<<endl<<endl;
cout<<" ==============================================================================="<<endl<<endl;
cout<<"\t\t\t Username: ";
getline(cin, username);
cout<<"\n\t\t\t Password: ";
do
{
pwc=getch();
if(pwc==13||pwc==' ')
{
break;
}
cout<<"*";
password+=pwc;
pw++;
}
while(pwc!=13||pwc!=' ');
n=username.length();
for(int a=0; a!=n; a++)
{
if(isupper(username.at(a)))
{
username.at(a) = tolower(username.at(a));
}
}
if(username=="admin" && password=="Admin")
{
system("CLS");
cout<<"\n ==============================================================================="<<endl<<endl;
cout<<"\t\t\t\t WELCOME ADMIN!"<<endl<<endl;
cout<<" ==============================================================================="<<endl<<endl;
Menu(username);
return main();
}
else if(username=="cashier" && password=="Cashier")
{
system("CLS");
cout<<"\n ==============================================================================="<<endl<<endl;
cout<<"\t\t\t\t WELCOME CASHIER!"<<endl<<endl;
cout<<" ==============================================================================="<<endl<<endl;
Menu(username);
}
else
{
system("CLS");
cout
<<"\n\t\t Invalid Username or Password. Please Try Again"<<endl;
return main();
}
system("pause");
return 0;
}
//
//
//
//
//
void Menu(string username)
{
head=NULL;
int menu;
if(username=="admin")
{
do
{
cout
<<"\t MENU: "
<<"\n\n\t\t\t A. FILE MAINTENANCE" <<endl
<<"\n\t\t\t\t 1. Display All Products"<<endl
<<"\t\t\t\t 2. Insert New Product"<<endl
<<"\t\t\t\t 3. Delete A Product" <<endl
<<"\t\t\t\t 4. Search A Product"<<endl
<<"\n\t\t\t B. SALES MODULE"<<endl
<<"\n\t\t\t\t 5. Cashier"<<endl
<<"\t\t\t\t 6. Print Receipt."<<endl
<<"\n\t\t\t C. EXIT"<<endl
<<"\n\t\t\t\t 7. Log Out"<<endl;
cout<<"\n\t Choose an option (1 to 7 only): ";
cin>>menu;
cin.ignore();
switch(menu)
{
case 1:
cout<<"\n ============================================================================="<<endl<<endl;
cout<<"\t\t\t DISPLAY ALL PRODUCTS"<<endl<<endl;
cout<<" ============================================================================="<<endl<<endl;
DisplayProduct();
system("pause");
system("CLS");
break;
case 2:
cout<<"\n ============================================================================="<<endl<<endl;
cout<<"\t\t\t INSERT NEW PRODUCT"<<endl;
cout<<" ============================================================================="<<endl<<endl;
InsertProduct();
break;
case 3:
cout<<"\n ============================================================================="<<endl<<endl;
cout<<"\t\t\t DELETE A PRODUCT"<<endl;
cout<<" ============================================================================="<<endl<<endl;
DeleteProduct();
break;
case 4:
cout<<"\n ============================================================================="<<endl<<endl;
cout<<"\t\t\t SEARCH A PRODUCT" <<endl;
cout<<" ============================================================================="<<endl<<endl;
SearchProduct();
break;
case 5:
cout<<"\n ============================================================================="<<endl<<endl;
cout<<"\t\t\t PURCHASE PRODUCTS"<<endl;
cout<<" ============================================================================="<<endl<<endl;
PurchaseProduct();
case 7:
cout<<"\n\n THANK YOU ADMIN!"<<endl<<endl;
break;
default:
cout<<"\n\n INVALID INPUT!"<<endl<<endl;
}
}while(menu!=7);
}
else
{
cout
<<"Choose an Option"
<<"\n1.Sales Module"
<<"\n2.Exit"<<endl;
cin>>menu;
switch(menu)
{
case 1:
cout<<"Sales Module";
break;
case 2:
cout<<"Exit!";
break;
default:
cout<<"Invalid Input!";
}
}
}
void InsertProduct()
{
product *newproduct;
newproduct=new product;
cout<<"\n\n\tEnter Product Name:";getline(cin,newproduct->pname);
cout<<"\n\tEnter Product Quantity:";cin>>newproduct->qnty;
cout<<"\n\tEnter Product ID:";cin>>newproduct->pid;
cout<<"\n\tEnter Product Price:";cin>>newproduct->price;
cin.ignore();
newproduct->ptr=NULL;
if(head==NULL)
head=newproduct;
else
{
product *lastproduct;
lastproduct=head;
while(lastproduct->ptr!=NULL)
lastproduct=lastproduct->ptr;
lastproduct->ptr=newproduct;
}
cout<<"\n\tProduct is inserted"<<endl;
system("pause");
system("CLS");
}
void DeleteProduct()
{
DisplayProduct();
product *current,*previous;
current=head;
int x;
if (current==NULL)
{
cout<<"\n\t\tNo Products Found.";
}
else
{
cout<<"\n\t\tEnter Product No. to delete:";
cin>>x;
int i=1;
if(x==1)
{
head=current->ptr;
delete current;
cout<<"\n\t\tProduct is deleted";
DisplayProduct();
}
else
{
while(i<x&¤t!=NULL)
{
previous=current;
current=current->ptr;
i=i++;
}
if(current==NULL)
{
cout<<"\n\t\tProduct does not Exist";
}
else
{
previous->ptr=current->ptr;
delete current;
cout<<"\n\t\tProduct is deleted";
DisplayProduct();
}
}
}
system("pause");
system("CLS");
}
void DisplayProduct()
{
product *current;
current=head;
int i=1;
cout<<setiosflags(ios::left);
while(current!=NULL)
{
cout<<"\n\t\t"<<setw(4)<<i;
cout<<setw(25)<<current->pname<<setw(3)<<current->qnty <<setw(3)<<current->pid <<setw(3)<<current->price;
current=current->ptr;
i++;
}
}
void SearchProduct()
{
string sid;//Name to search
product *current;
current=head;
if (current==NULL)
cout<<"\n\t\tNo Products Found";
else
{
cout<<"\n\t\tEnter product to search: ";
cin>>sid;
int i=1;
while(sid.compare(current->pid))
{
current=current->ptr;
if (current==NULL)
break;
i++;
}
if(current!=NULL)
cout<<"\n\t\t"<<current->pid<<" is Product No."<<i;
else
cout<<"\n\t\t"<<sid<<" is not in the list.";
DisplayProduct();
cout<<endl;
}
system("pause");
system ("CLS");
}
void PurchaseProduct()
{
}
Your data is in memory until you save it to hardk disk, so you need to implement some mechanism for doing that.
You can choose among several options such as using data bases, plain files(XML, YAML, JSON, etc ...) and object searialization wich I think it's your best option right now. There are several libraries out there for doing that, but for start I recommend you use this tutorial. Then move to A practical guide to C++ serialization.
I recommend serialization because I realized you are just starting with C++. And others options like data bases and formated files(xml, Yaml, etc...) require more complex code.
Have fun!!! After you read all this stuff and (try to put it in practice), if you still can't solve your problem, come back here, and we will happy to help you.