Error after execution of c++ code - c++

After execution, I am getting following errors in Visual C++ 2015:
while (c< pos - 1)
error: identifier c undefined
clrscr();
error: identifier clrscr(); is undefined
case 6: display(head);
error: identifier display is undefined
Here is my code:
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
struct library
{
char author[20], title[20], pub[20];
int price;
library *next;
};
int sum = 0;
void main()
{
clrscr();
library *head = NULL;
library *initial(void);
library *purchase(library *);
void stock(library *);
void search(library *);
int choice;
while (1)
{
cout << "Mtavari\n";
cout << "1) Sawyisi monacemebi\n";
cout << "2) Yidvebi\n";
cout << "3) Gayidvebi\n";
cout << "4) Wignebi\n";
cout << "5) Dzieba\n";
cout << "6) Wignebis nuskha\n";
cout << "7) Gamosvla\n";
cout << "Airchiet:-";
cin >> choice;
switch (choice)
{
case 1: head = initial();
getch();
break;
case 2: head = purchase(head);
getch();
break;
getch();
break;
case 5: search(head);
getch();
break;
case 6: display(head);
getch();
break;
case 7: goto out;
default: cout << "\nShecdomiti archevani\n";
}
clrscr();
}
out:
}
library *initial(void)
{
clrscr();
library *newl = NULL, *start = NULL, *end = newl;
char ch;
while (1)
{
cout << "\n\nSheiyvanet y an Y\n";
cout << "Gsurt archeva:-";
cin >> ch;
if (ch == 'y' || ch == 'Y')
{
newl = new library;
cout << "\n\nSheiyvanet wignis avtori:-";
cin >> newl->author;
cout << "Sheiyvanet satauri:-";
cin >> newl->title;
cout << "Sheiyvanet gamomcemloba:-";
cin >> newl->pub;
cout << "Sheiyvanet fasi:-";
cin >> newl->price;
sum = sum + newl->price;
if (start == NULL)
start = newl;
else
end->next = newl;
end = newl;
end->next = NULL;
}
else break;
}
return(start);
}
library *purchase(library *start)
{
clrscr();
int pos, count = 1, choice;
library *newl, *cnt = start, *head = start;
if (start == NULL)
cout << "\n\nMonacemebi ar aris\n";
cout << "\n\nMtavari\n";
cout << "1) Pirvel adgilze sheyvana\n";
cout << "2) Shuashi chamateba\n";
cout << "3) Bolo poziciaze sheyvana \n";
cout << "4) Gamosvla\n";
cout << "Airchiet:-";
cin >> choice;
if (choice >= 1 && choice <= 3)
{
newl = new library;
cout << "Avtoris saxeli :-";
cin >> newl->author;
cout << "Wignis satauri :-";
cin >> newl->title;
cout << "Gamomcemloba :-";
cin >> newl->pub;
cout << "Fasi:-";
cin >> newl->price;
sum = sum + newl->price;
}
switch (choice)
{
case 1:
newl->next = head;
head = newl;
break;
case 2:
read:
cout << "\n\nAirchiet pozicia:-";
cin >> pos;
while (cnt != NULL)
{
count++;
cnt = cnt->next;
}
if (pos<1 || pos>count + 1)
{
cout << "\n\nPozicia arasworia\n";
goto read;
}
{
while (c<pos - 1)
{
c++;
start = start->next;
}
}
newl->next = start->next;
start->next = newl;
break;
case 3:
start = start->next;
start->next = newl;
newl->next = NULL;
break;
case 4: goto out;
default: cout << "\nArchevani arasworia\n";
break;
}
out:
return(head);
}
void stock(library *start)
{
clrscr();
int count = 0;
while (start != NULL)
{
count++;
start = start->next;
}
cout << "\n\n\n\tWignebis raodenoba " << count << endl;
cout << "\tMtliani fasi " << sum;
}
void search(library *start)
{
clrscr();
char author[20], title[20];
cout << "Sheiyvanet sadziebo fraza(avtori an wigni...)\n";
cin >> title >> author;
while (start != NULL)
{
if (title == start->title)
{
if (author == start->author)
{
cout << "\n\nArsebuli wignebi\n";
cout << "Girebuleba" << start->price;
return;
}
}
}
cout << "\n\nVer moidzebna\n";
}
void display(library *start)
{
clrscr();
cout << setw(10) << "Satauri" << setw(25) << "Avtori" << setw(25) << "Publikacia" << setw(20) << "Fasi" << endl << endl;
for (int i = 0;i<40;i++)
cout << "=*";
cout << endl;
while (start != NULL)
{
cout << setw(10) << start->title << setw(25) << start->author << setw(25) << start->pub << setw(20) << start->price << endl;
start = start->next;
}
}

At first, clean-up your code (I see, you posted HTML-formated code. So, "thanks" for a challenge to save as HTML-file, open in a browser, then copy-paste away as plain text).
For now, I'm able to tell you what's wrong:
replace void main() with int main(), void main() is legacy and no more correct. Also, you must return 0; at end or return any other number if you wish to tell that your program finished with errors (for example, missing argument or not existing file).
Don't put function prototypes inside main(), put them over main!
I mean:
library *initial(void);
library *purchase(library *);
void stock(library *);
void search(library *);
int main()
{
//...
clrsrc() function is no more exist. Instead, you have to use this function: How do we clear the console in assembly?
missing display() function is caused you forgot to put prototype to it over main() function: void display(library *start);
missing c in while(c < pos - 1): just put int c = 0; over that while loop.
I have been removed using namespace std; and added the std:: to every function requires it.
Tip: Instead of having << std::endl << std::endl you can just do << "\n\n"
So, the full and fixed code will be:
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#ifdef _WIN32
// for Windows
#include <conio.h>
#include <windows.h>
#else
// for Linux (required to install the ncurses-dev and link with -lncurses!)
#include <ncurses.h>
#endif
void clrscr()
{
#ifdef _WIN32
// for Windows
char fill = ' ';
COORD tl = {0,0};
CONSOLE_SCREEN_BUFFER_INFO s;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(console, &s);
DWORD written, cells = s.dwSize.X * s.dwSize.Y;
FillConsoleOutputCharacter(console, fill, cells, tl, &written);
FillConsoleOutputAttribute(console, s.wAttributes, cells, tl, &written);
SetConsoleCursorPosition(console, tl);
#else
// for Linux
system("clear");
#endif
}
struct library
{
char author[20], title[20], pub[20];
int price;
library *next;
};
library *initial(void);
library *purchase(library *);
void stock(library *);
void search(library *);
void display(library *start);
int sum = 0;
int main()
{
clrscr();
library *head = NULL;
int choice;
while (1)
{
std::cout << "Mtavari\n";
std::cout << "1) Sawyisi monacemebi\n";
std::cout << "2) Yidvebi\n";
std::cout << "3) Gayidvebi\n";
std::cout << "4) Wignebi\n";
std::cout << "5) Dzieba\n";
std::cout << "6) Wignebis nuskha\n";
std::cout << "7) Gamosvla\n";
std::cout << "Airchiet:-";
std::cin >> choice;
switch (choice)
{
case 1: head = initial();
getch();
break;
case 2: head = purchase(head);
getch();
break;
getch();
break;
case 5: search(head);
getch();
break;
case 6: display(head);
getch();
break;
case 7:
goto out;
default: std::cout << "\nShecdomiti archevani\n";
}
clrscr();
}
out:
return 0;
}
library *initial(void)
{
clrscr();
library *newl = NULL, *start = NULL, *end = newl;
char ch;
while (1)
{
std::cout << "\n\nSheiyvanet y an Y\n";
std::cout << "Gsurt archeva:-";
std::cin >> ch;
if (ch == 'y' || ch == 'Y')
{
newl = new library;
std::cout << "\n\nSheiyvanet wignis avtori:-";
std::cin >> newl->author;
std::cout << "Sheiyvanet satauri:-";
std::cin >> newl->title;
std::cout << "Sheiyvanet gamomcemloba:-";
std::cin >> newl->pub;
std::cout << "Sheiyvanet fasi:-";
std::cin >> newl->price;
sum = sum + newl->price;
if (start == NULL)
start = newl;
else
end->next = newl;
end = newl;
end->next = NULL;
}
else break;
}
return(start);
}
library *purchase(library *start)
{
clrscr();
int pos, count = 1, choice;
library *newl, *cnt = start, *head = start;
if (start == NULL)
std::cout << "\n\nMonacemebi ar aris\n";
std::cout << "\n\nMtavari\n";
std::cout << "1) Pirvel adgilze sheyvana\n";
std::cout << "2) Shuashi chamateba\n";
std::cout << "3) Bolo poziciaze sheyvana \n";
std::cout << "4) Gamosvla\n";
std::cout << "Airchiet:-";
std::cin >> choice;
if (choice >= 1 && choice <= 3)
{
newl = new library;
std::cout << "Avtoris saxeli :-";
std::cin >> newl->author;
std::cout << "Wignis satauri :-";
std::cin >> newl->title;
std::cout << "Gamomcemloba :-";
std::cin >> newl->pub;
std::cout << "Fasi:-";
std::cin >> newl->price;
sum = sum + newl->price;
}
switch (choice)
{
case 1:
newl->next = head;
head = newl;
break;
case 2:
read:
std::cout << "\n\nAirchiet pozicia:-";
std::cin >> pos;
while (cnt != NULL)
{
count++;
cnt = cnt->next;
}
if (pos<1 || pos>count + 1)
{
std::cout << "\n\nPozicia arasworia\n";
goto read;
}
{
int c = 0;
while(c < pos - 1)
{
c++;
start = start->next;
}
}
newl->next = start->next;
start->next = newl;
break;
case 3:
start = start->next;
start->next = newl;
newl->next = NULL;
break;
case 4:
goto out;
default:
std::cout << "\nArchevani arasworia\n";
break;
}
out:
return(head);
}
void stock(library *start)
{
clrscr();
int count = 0;
while (start != NULL)
{
count++;
start = start->next;
}
std::cout << "\n\n\n\tWignebis raodenoba " << count << std::endl;
std::cout << "\tMtliani fasi " << sum;
}
void search(library *start)
{
clrscr();
char author[20], title[20];
std::cout << "Sheiyvanet sadziebo fraza(avtori an wigni...)\n";
std::cin >> title >> author;
while (start != NULL)
{
if (title == start->title)
{
if (author == start->author)
{
std::cout << "\n\nArsebuli wignebi\n";
std::cout << "Girebuleba" << start->price;
return;
}
}
}
std::cout << "\n\nVer moidzebna\n";
}
void display(library *start)
{
clrscr();
std::cout << std::setw(10) << "Satauri" << std::setw(25) << "Avtori" << std::setw(25) << "Publikacia" << std::setw(20) << "Fasi" << "\n\n";
for (int i = 0;i<40;i++)
std::cout << "=*";
std::cout << std::endl;
while (start != NULL)
{
std::cout << std::setw(10) << start->title << std::setw(25) << start->author << std::setw(25) << start->pub << std::setw(20) << start->price << std::endl;
start = start->next;
}
}
I hope this will help you.

c/c++ is a single-pass language. you have to define functions before they're used. i.e. higher up. The explains why display isn't recognized: you need to either have the function body above where it's used or put an empty declaration in:
void display(library *start);
before it's first used. this tells the compiler what sort of object "display" is. As for clrscr, that depends on the library you're using. check the references for your library.
As for c, you never made a variable called "c", so the compiler doesn't know what that's supposed to be. Neither do we.

Related

C++ Run error with wild characters; no compile and build errors

I am using Code::Blocks to write this C++ program, but everytime I do a 3rd Insert on the Link List, and when I use the Display option to check my work, I get a strange error. I have encountered no build errors. Please help check and advise when you have the chance, thank you.
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
typedef struct student
{
string name;
int quiz1, quiz2, quiz3;
struct student *nxt;
} STUDENT;
class studentsStruct
{
private:
STUDENT *S; //Header of the Student List
public:
void init();
void add(string n, int a, int b, int c);
void del(string n);
void update(string n);
void save();
void retrieve();
void display();
};
int menu();
int updatemenu();
float round2(float x); // function to round the average and limit to 2 decimal points
int main()
{
studentsStruct ars;
string nm;
int a, b, c;
ars.init(); // Initialize the S Header of the Student List
ars.retrieve(); // Check if the "studentsdb.txt" is available and load the data as needed
while (1)
{
switch (menu())
{
case 1:
system("cls");
cout << "Insert Mode" << endl;
cout << "Name: ";
cin >> nm;
cout << "Input Quiz 1: ";
cin >> a;
cout << "Input Quiz 2: ";
cin >> b;
cout << "Input Quiz 3: ";
cin >> c;
ars.add(nm, a, b, c);
break;
case 2:
system("cls");
cout << "Update Mode" << endl;
cout << "Input Students Name: ";
cin >> nm;
ars.update(nm);
break;
case 3:
system("cls");
cout << "Delete Mode" << endl;
cout << "Enter Name: ";
cin >> nm;
ars.del(nm);
break;
case 4:
ars.display();
break;
case 5:
ars.save();
exit(0);
default:
cout << "1 to 5 only. ";
system("pause");
}
}
return 0;
}
void studentsStruct::init()
{
S = NULL;
}
void studentsStruct::add(string n, int a, int b, int c)
{
STUDENT *p, *q, *temp;
p = q = S;
temp = new STUDENT;
temp->name = n;
temp->quiz1 = a;
temp->quiz2 = b;
temp->quiz3 = c;
while (p != NULL)
{
q = p;
p = p->nxt;
}
if (p == S)
{
S = temp;
}
else
{
q->nxt = temp;
temp->nxt = p;
}
}
void studentsStruct::del(string n)
{
STUDENT *p, *q;
p = q = S;
while (p != NULL && p->name != n)
{
q = p;
p = p->nxt;
}
if (p == NULL)
{
cout << n << (" is not found.") << endl;
system("pause");
}
else
{
if (p == S)
{
S = S->nxt;
}
else
{
q->nxt = p->nxt;
delete (p);
cout << n << (" is deleted from the student records") << endl;
}
}
}
void studentsStruct::update(string n)
{
STUDENT *p, *q;
p = q = S;
int ua, ub, uc;
while (p != NULL && p->name != n)
{
q = p;
p = p->nxt;
}
if (p == NULL)
{
cout << ("Not Found") << endl;
system("pause");
}
else
{
while (1)
{
cout << "Update Mode" << endl;
cout << "Student : " << p->name << endl;
cout << "Quiz 1 Grade: " << p->quiz1 << endl;
cout << "Quiz 2 Grade: " << p->quiz2 << endl;
cout << "Quiz 3 Grade: " << p->quiz3 << endl;
switch (updatemenu())
{
case 1:
cout << "Quiz 1 Grade: ";
cin >> ua;
p->quiz1 = ua;
save();
break;
case 2:
cout << "Quiz 2 Grade: ";
cin >> ub;
p->quiz2 = ub;
save();
break;
case 3:
cout << "Quiz 3 Grade: ";
cin >> uc;
p->quiz3 = uc;
save();
break;
case 4:
main();
save();
break;
default:
cout << "Select an option between 1 to 3" << endl;
cout << "or select 4 to exit the Update Menu: " << endl;
}
}
}
}
int updatemenu()
{
int um;
cout << "Update Menu" << endl;
cout << "1.) Update Quiz 1 Grade" << endl;
cout << "2.) Update Quiz 2 Grade" << endl;
cout << "3.) Update Quiz 3 Grade" << endl;
cout << "4.) Return to the Main Menu" << endl;
cout << "Select an option between 1 to 3" << endl;
cout << "or select 4 to exit the Update Menu: ";
cin >> um;
return (um);
}
void studentsStruct::display()
{
STUDENT *p;
int i = 1;
p = S;
float ave;
string remarks;
system("cls");
cout << "No.\tName\tQuiz 1\tQuiz 2\tQuiz 3\tAverage\tRemarks\n";
while (p != NULL)
{
ave = (p->quiz1 + p->quiz2 + p->quiz3) / 3.0;
remarks = ave >= 75 ? "Passed" : "Failed";
cout << i++ << ".)\t" << p->name << "\t" << p->quiz1 << "\t" << p->quiz2 << "\t" << p->quiz3 << "\t" << round2(ave) << "\t" << remarks << endl;
p = p->nxt;
}
system("pause");
}
int menu()
{
int op;
cout << "Menu" << endl;
cout << "1.) Insert" << endl;
cout << "2.) Update" << endl;
cout << "3.) Delete" << endl;
cout << "4.) Display" << endl;
cout << "5.) Exit" << endl;
cout << "Select(1-5): ";
cin >> op;
return (op);
}
// Save the student data to "studentdb.txt" if available and create a file if needed
void studentsStruct::save()
{
STUDENT *p;
p = S;
ofstream stuData;
stuData.open("studentdb.txt");
while (p != NULL)
{
stuData << p->name << " " << p->quiz1 << " " << p->quiz2 << " " << p->quiz3 << endl;
p = p->nxt;
}
stuData.close();
}
// Check if the "studentdb.txt" is available and load the data as needed
void studentsStruct::retrieve()
{
ifstream fp;
string n;
int ax, by, cz;
fp.open("studentdb.txt");
if (fp.peek() != std::ifstream::traits_type::eof())
{
while (fp >> n >> ax >> by >> cz)
{
add(n, ax, by, cz);
}
}
else
{
cout << "The file is available but contains no records" << endl;
}
fp.close();
}
// function to round the average and limit to 2 decimal points
float round2(float x)
{
return trunc(x * 100.0) / 100.0;
}
And this is the error I am encountering (see screenshot below):

read data from a file in a double linked list

I created a simple doubly linked list to store data about the plant. I needed to make the data write and read from a file c++. There were no problems with writing to the file. But I have no idea how to properly read data from a file(. Maybe there are some features.
Earlier, when I wrote data from a file to an array, I sorted through the elements using a loop, by index. but now when I need to write the elements in a double linked list - I really do not understand what to do.
Here is my code:
#include <iostream>
#include <cstring>
#include <list>
#include <fstream>
#include <locale>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <list>
using namespace std;
typedef struct
{
char plant[100];
char family[100];
char species[100];
}BOOK;
typedef struct tag_obj
{
BOOK b;
struct tag_obj* prev, * next;
}OBJ;
OBJ* head = NULL, * tail = NULL;
void add_obj(OBJ* obj, BOOK book, char sim[50]) {
OBJ* c = head;
while (c != NULL) {
if (strcmp(c->b.plant, sim) == 0){
cout<< " element already existst(("<< endl;
return ;
}
c = c->next;
}
OBJ* ptr = new OBJ;
ptr->b = book;
ptr->prev = obj;
ptr->next = (obj == NULL) ? NULL : obj->next;
if (obj != NULL) {
obj->next = ptr;
if (obj->next != NULL) obj->next->prev = ptr;
}
if (ptr->prev == NULL) head = ptr;
if (ptr->next == NULL) tail = ptr;
}
void del_obj(OBJ* obj) {
char x[50];
cout << "enter the name of the plant whose data you want to delete: ";
cin >> x;
OBJ* tmp;
OBJ* h;
if (head == NULL)
{
cout << "List empty,nothing to delete" << endl;
return;
}
if (strcmp(head->b.plant, x) == 0)
{
tmp = head;
head = head->next;
head->prev = NULL;
cout << "Element Deleted" << endl;
free(tmp);
return;
}
h = head;
while (h->next->next != NULL)
{
if (strcmp(h->next->b.plant, x) == 0)
{
tmp = h->next;
h->next = tmp->next;
tmp->next->prev = h;
cout << "Element Deleted" << endl;
free(tmp);
return;
}
h = h->next;
}
if (strcmp(h->next->b.plant, x) == 0)
{
tmp = h->next;
free(tmp);
h->next = NULL;
cout << "Element Deleted" << endl;
return;
}
cout << "Element " << x << " not found" << endl;
}
void show() {
OBJ* c = head;
int d = 1;
while (c != NULL) {
cout << d << ")" << " " << c->b.plant << " " << c->b.family << " " << c->b.species << endl;
c = c->next;
d++;
}
}
int main() {
//setlocale(LC_ALL, "ukr");
ofstream fout;
fout.open("plant account.txt", ios::app);
ifstream fin;
fin.open("plant account.txt");
if (!fout.is_open())
{
cout << "Помилка вiдкриття файлу";
return 1;
}
char sim[50];
BOOK book = { " PLANT "," FAMILY " };
//add_obj(tail, book);
int choice;
cout << "**plant account**" << endl;
cout << "1(add an element)" << endl;
cout << "2(delete)" << endl;
cout << "3(show)" << endl;
cout << "4(exit)" << endl << endl;
start:
cout << " Choose action : ";
cin >> choice;
switch (choice)
{
case 1:
fout << endl;
cout << "Plant name: ";
cin >> book.plant;
strcpy_s(sim, book.plant);
fout << book.plant << " ";
cout << "Plant family: ";
cin >> book.family;
fout << book.family << " ";
cout << "Plant species: ";
cin >> book.species;
fout << book.species << " ";
add_obj(tail, book,sim);
cout << endl;
goto start;
case 2:
del_obj(head);
cout << endl;
goto start;
case 3:
show();
cout << endl;
goto start;
case 4:
cout << " " << endl;
exit(222);
default:
break;
}
fout.close();
fin.close();
return 0;
}

I am getting the linking errors in my C++ program, I could not compile my code

Error 1 error LNK2005: "class bus bs" (?bs##3Vbus##A) already defined in bus.obj c:\Users\tahir\documents\visual studio 2013\Projects\Project17\Project17\main.obj Project17
Error 2 error LNK2005: "class ticket tick" (?tick##3Vticket##A) already defined in bus.obj c:\Users\tahir\documents\visual studio 2013\Projects\Project17\Project17\main.obj Project17
Error 3 error LNK2005: "class ticket tick" (?tick##3Vticket##A) already defined in bus.obj c:\Users\tahir\documents\visual studio 2013\Projects\Project17\Project17\ticket.obj Project17
Error 4 error LNK2005: "class bus bs" (?bs##3Vbus##A) already defined in bus.obj c:\Users\tahir\documents\visual studio 2013\Projects\Project17\Project17\ticket.obj Project17
Error 5 error LNK1169: one or more multiply defined symbols found c:\users\tahir\documents\visual studio 2013\Projects\Project17\Debug\Project17.exe 1 1 Project17
bus.h
#pragma once
class bus
{
int busno;
int noofkidsseats;
int noofwomenseats;
int noofmenseats;
int noofspecialseats;
int noofvvip;
char *busname;
char *startpoint;
char *destination;
public:
bus();
void input();
int rbusno();
int rnoofkidsseats();
int rnoofwomenseats();
int rnoofmenseats();
int rnoofspecialseats();
int rnoofvvip();
void display();
}bs;
bus.cpp
#include <iostream>
#include <fstream>
#include "bus.h"
#include "ticket.h"
#include "windows.h"
using namespace std;
int length(char arr[])
{
int i = 0;
for (; arr[i] != '\0'; i++);
return i;
}
void gotoxy(int x, int y)
{
static HANDLE h = NULL;
if (!h)
h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD c = { x, y };
SetConsoleCursorPosition(h, c);
}
bus::bus()
{
busno = 0;
noofkidsseats = 0;
noofwomenseats = 0;
noofmenseats = 0;
noofspecialseats = 0;
noofvvip = 0;
busname = '\0';
startpoint = '\0';
destination = '\0';
}
int bus::rbusno()
{
return busno;
}
int bus::rnoofkidsseats()
{
return noofkidsseats;
}
int bus::rnoofwomenseats()
{
return noofwomenseats;
}
int bus::rnoofmenseats()
{
return noofmenseats;
}
int bus::rnoofspecialseats()
{
return noofspecialseats;
}
int bus::rnoofvvip()
{
return noofvvip;
}
void bus::input()
{
cout << "ENTER THE BUS NUMBER: ";
cin >> busno;
cout << "ENTER THE NUMBER OF SEATS FOR KIDS: ";
cin >> noofkidsseats;
cout << "ENTER THE NUMBER OF SEATS FOR WOMEN: ";
cin >> noofwomenseats;
cout << "ENTER THE NUMBER OF SEATS FOR MEN: ";
cin >> noofmenseats;
cout << "ENTER THE NUMBER OF SEATS SPECIAL PERSONS: ";
cin >> noofspecialseats;
cout << "ENTER THE NUMBER OF SEATS FOR VVIP PASSENGERS: ";
cin >> noofvvip;
cout << "ENTER THE BUS NAME: ";
char name[20];
cin >> name;
int l = length(name);
busname = new char[l];
int i;
for (i = 0; i < l; i++)
busname[i] = name[i];
busname[i] = '\0';
cout << "ENTER THE STARTING POINT: ";
char start[20];
cin >> start;
int L = length(start);
startpoint = new char[L];
int j;
for (j = 0; j < L; j++)
startpoint[j] = start[j];
startpoint[j] = '\0';
cout << "ENTER THE DESTINATION: ";
char end[20];
cin >> end;
int s = length(end);
destination = new char[s];
int k;
for (k = 0; k < s; k++)
destination[k] = end[k];
destination[k] = '\0';
}
void bus::display()
{
cout << " ***************************************" << endl;
cout << " * *" << endl;
cout << " * BUS DEATAILS *" << endl;
cout << " * *" << endl;
cout << " ***************************************" << endl;
cout << " THE BUS NUMBER: ";
cout << busno << endl;
cout << " THE BUS NAME: ";
cout << busname << endl;
cout << " STARTING POINT: ";
cout << startpoint << endl;
cout << " DESTINATION: ";
cout << destination << endl;
cout << " THE NUMBER OF SEATS FOR KIDS: ";
cout << noofkidsseats << endl;
cout << " THE NUMBER OF SEATS FOR WOMEN: ";
cout << noofwomenseats << endl;
cout << " THE NUMBER OF SEATS FOR MEN: ";
cout << noofmenseats << endl;
cout << " THE NUMBER OF SEATS SPECIAL PERSONS: ";
cout << noofspecialseats << endl;
cout << " THE NUMBER OF SEATS FOR VVIP PASSENGERS: ";
cout << noofvvip << endl;
}
ticket.h
#pragma once
class ticket
{
int resno;
int age;
int tokids;
int towomen;
int tomen;
int tospecial;
int tovvip;
int noofkidsseats;
int noofwomenseats;
int noofmenseats;
int noofspecialseats;
int noofvvip;
char *pname;
char *status;
public:
ticket();
void reservation();
int returnresno();
void cancellation();
void print();
}tick;
ticket.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "ticket.h"
#include "bus.h"
using namespace std;
ticket::ticket()
{
resno = 0;
age = 0;
tokids = 0;
towomen = 0;
tomen = 0;
tospecial = 0;
tovvip = 0;
pname = '\0';
status = '\0';
}
int ticket::returnresno()
{
return resno;
}
void ticket::print()
{
int f = 0;
system("cls");
ifstream fn("Ticket1.dat", ios::out); fn.seekg(0);
if (!fn)
{
cout << "ERROR IN THE FILE ";
}
X:
cout << "ENTER THE RESERVATION NO ";
int n;
cin >> n;
while (!fn.eof())
{
fn.read((char*)&tick, sizeof(tick));
if (n == resno)
{
f = 1;
system("cls");
cout << "NAME: ";
cout << pname;
cout << "AGE: ";
cout << age;
cout << "PRESENT STATUS: ";
cout << status;
cout << "RESERVATION NUMBER: ";
cout << resno;
cout << "PRESS ANY KEY TO CONTINUE ";
system("pause");
}
}
if (f == 0)
{
system("cls");
cout << "UNRECOGINIZED RESERVATION NO !!! WANNA RETRY ? (Y / N) ";
char a;
cin >> a;
if (a == 'y' || a == 'Y')
{
system("cls");
goto X;
}
else
{
cout << "PRESS ANY KEY TO CONTINUE";
system("pause");
}
}
fn.close();
}
void ticket::reservation()
{
system("cls");
cout << "RESERVATION ";
cout << "ENTER THE BUS NO: ";
int bno, f = 0; cin >> bno; ofstream file;
ifstream fin("bus1.dat", ios::out); fin.seekg(0);
if (!fin)
{
system("cls");
cout << "ERROR IN THE FILE ";
system("cls");
while (!fin.eof())
{
fin.read((char*)&bs, sizeof(bs)); int z;
z = bs.rbusno();
if (bno == z)
{
f = 1;
noofkidsseats = bs.rnoofkidsseats();
noofwomenseats = bs.rnoofwomenseats();
noofmenseats = bs.rnoofmenseats();
noofspecialseats = bs.rnoofspecialseats();
noofvvip = bs.rnoofvvip();
}
}
if (f == 1)
{
file.open("Ticket1.dat", ios::app);
S:
system("cls");
cout << "NAME:";
cin >> pname;
cout << "AGE:";
cin >> age;
system("cls");
cout << "SELECT THE CATEGORY WHICH YOU WISH TO TRAVEL";
cout << "1.KIDS CATEGORY: ";
cout << "2.WOMEN CATEGORY: ";
cout << "3.MEN CATEGORY: ";
cout << "4.SPECIAL CATEGORY: ";
cout << "5.SECOND CLASS SLEEPER: ";
cout << "ENTER YOUR CHOICE ";
int c;
cin >> c;
switch (c)
{
case 1:
tokids++;
resno = rand();
if ((noofkidsseats - tokids)>0)
{
status = "confirmed";
cout << "STATUS";
puts(status);
cout << "RESERVATION NO: ";
cout << resno;
system("pause");
file.write((char*)&tick, sizeof(tick)); break;
status = "pending";
cout << "STATUS";
puts(status);
cout << "RESERVATION NO";
cout << resno;
system("pause");
file.write((char*)&tick, sizeof(tick)); break;
}
case 2:
towomen++;
resno = rand();
if ((noofwomenseats - towomen)>0)
{
status = "confirmed";
cout << "STATUS";
puts(status);
cout << "RESERVATION NO: ";
cout << resno;
system("pause");
file.write((char*)&tick, sizeof(tick)); break;
status = "pending";
cout << "STATUS";
puts(status);
cout << "RESERVATION NO:";
cout << resno;
system("pause");
file.write((char*)&tick, sizeof(tick)); break;
}
case 3:
tomen++;
resno = rand();
if ((noofmenseats - tomen)>0)
{
status = "confirmed";
cout << "STATUS";
puts(status);
cout << "RESERVATION NO: ";
cout << resno;
system("pause");
file.write((char*)&tick, sizeof(tick)); break;
}
else
{
status = "pending";
cout << "STATUS";
puts(status);
cout << "RESERVATION NO: ";
cout << resno;
system("pause");
file.write((char*)&tick, sizeof(tick)); break;
}
case 4:
tospecial++;
resno = rand();
if ((noofspecialseats - tospecial)>0)
{
status = "confirmed";
cout << "STATUS";
puts(status);
cout << "RESERVATION NO";
cout << resno;
system("pause");
file.write((char*)&tick, sizeof(tick)); break;
}
else
{
status = "pending";
cout << "STATUS";
puts(status);
cout << "RESERVATION NO";
cout << resno;
system("pause");
file.write((char*)&tick, sizeof(tick)); break;
}
case 5:
tovvip++;
resno = rand();
if ((noofvvip - tovvip)>0)
{
status = "confirmed";
cout << "STATUS";
puts(status);
cout << "RESERVATION NO";
cout << resno;
system("pause");
file.write((char*)&tick, sizeof(tick)); break;
}
else
{
status = "pending";
cout << "STATUS";
puts(status);
cout << "RESERVATION NO";
cout << resno;
system("pause");
file.write((char*)&tick, sizeof(tick)); break;
}
}
cout << "DO YOU WISH TO CONTINUE BOOKING TICKETS (Y/N) ? ";
char n;
cin >> n;
if (n == 'y' || n == 'Y')
{
goto S;
}
}
}
if (f == 0)
{
system("cls");
cout << "ERROR IN THE BUS NUMBER ENTERED !!!";
system("pause");
}
file.close();
}
void ticket::cancellation()
{
system("cls");
ifstream fin;
fin.open("Ticket1.dat", ios::out);
ofstream file;
file.open("Temp1.dat", ios::app);
fin.seekg(0);
cout << "ENTER THE RESERVATION NO: ";
int r, f = 0;
cin >> r;
if (!fin)
{
cout << "ERROR IN THE FILE !!!";
}
while (!fin.eof())
{
fin.read((char*)&tick, sizeof(tick)); int z;
z = returnresno();
if (z != r)
{
file.write((char*)&tick, sizeof(tick));
}
if (z == r)
{
f = 1;
}
}
file.close(); fin.close();
remove("Ticket1.dat");
rename("Temp1.dat", "Ticket1.dat");
if (f == 0)
{
cout << "NO SUCH RESERVATION IS MADE !!! PLEASE RETRY ";
system("pause");
}
else
{
cout << "RESERVATION CANCELLED";
system("pause");
}
}
main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "bus.h"
#include "ticket.h"
using namespace std;
int main()
{
ticket obj;
bus obj1;
int ch, r = 1000, j;
cout << "WELCOME";
Z:
cout << "BUS TICKET RESERVATION";
cout << "==========================";
cout << "1.BUS DETAILS";
cout << "2.UPDATE BUS DETAILS ";
cout << "3.RESERVING A TICKET ";
cout << "4.CANCELLING A TICKET";
cout << "5.DISPLAY THE PRESENT TICKET STATUS ";
cout << "6.EXIT";
cout << "ENTER YOUR CHOICE: ";
cin >> ch;
char n;
switch (ch)
{
case 1:
{
ifstream fin("bus1.dat", ios::out);
fin.seekg(0);
if (!fin)
{
cout << "ERROR IN THE FILE !!!";
}
else
{
while (!fin.eof())
{
fin.read((char*)&obj1, sizeof(obj1));
bs.display();
}
}
fin.close();
goto Z;
}
case 2:
{
cout << "ENTER THE PASSWORD ";
cin >> j;
cout << "CHECKING PLEASE WAIT ";
system("pause");
Y:
ofstream fout("bus1.dat", ios::app);
bs.input();
fout.write((char*)&obj1, sizeof(obj1));
fout.close();
cout << "DO YOU WISH TO CONTINUE UPDATING ?(Y/N)";
cin >> n;
if (n == 'y' || n == 'Y')
{
goto Y;
goto Z;
}
else
{
goto Z;
}
}
case 3:
{
obj.reservation();
goto Z;
}
case 4:
{
obj.cancellation();
goto Z;
}
case 5:
{
obj.print();
goto Z;
}
case 6:
{
exit(0);
}
}
system("pause");
return 0;
}
class bus
{
// members
}bs;
Not only declares a type bus but also a defines a variable bs. The same for tick.
When you include these headers in several cpp files, you get several definitions of these variables.
I'm sure none of the C++ books you have read told you to define variables like this in header files, so just don't. Declare the types in the headers and the variables where you need them, likely in the cpp files.

output within compiler and from exe file differs

i am using turbo c++. when i run the following code from compiler i get different answer at marked point in function result(int) then what i get from running .exe file created.
#include<fstream.h>
#include<conio.h>
#include<process.h>
#include<iomanip.h>
#include<string.h>
#include<stdio.h>
#include<dos.h>
ifstream fil;
int pos[50];
char date[11];
void exitt(int times = 0)
{
cout << endl << endl << " Enter 0 to exit." << endl;
if (times == 0)
cout << " Enter L to return to last screen." << endl;
}
void options();
void companychoose();
void companyscreen(int);
void write(int ch, int pos = 0) //add a check for duplicacy
{
ofstream fout;
clrscr();
if (ch == 1)
{
fout.open("database.dat", ios::binary | ios::app | ios::ate);
char companyname[20], temp;
exitt();
cout << " Enter Company name: ";
gets(companyname);
if (strcmp(companyname, "0") == 0)
exit(0);
else if (strcmp(companyname, "l") == 0)
options();
for (int i = 19; i>0; i--)
companyname[i] = companyname[i - 1];
companyname[0] = '%';
fout << endl;
fout << companyname;
fout.close();
cout << " Add data now?(y/n)" << endl;
askagain:
cin >> temp;
switch (temp)
{
case 'y':
fil.close();
write(2);
break;
case 'n':
options();
break;
default:
cout << " Invalid input" << endl;
goto askagain;
break;
}
}
}
void result(int ch)
{
int high[4], low[4], end, i = 0, enough = 0, temp = 0;
char check[20];
fil.open("database.dat", ios::binary);
fil.seekg(pos[ch], ios::beg);
fil >> check;
cout << endl;
if (check[0] == '%')
{
cout << " Not Enough Data!!!" << endl;
fil.close();
return;
}
while (!fil.eof())
{
if (i == 3)
{
i = 0;
enough = 1;
}
fil >> high[i] >> low[i] >> end >> check;
if (check[0] == '%')
break;
i++;
}
low[i] = 0;
temp = low[0];
if (enough == 0)
cout << " Not Enough Data!!!" << endl;
else
{
for (i = 0; i<3; i++)
{
if (low[i]<low[i + 1])
temp = low[i + 1];
}
if (temp>end)
cout << " Stock Running Low!!";
else if (temp = end)
cout << " Stock Is Stable";
else
cout << " Stock is HIGH!!";
cout << " " << end - temp << endl << endl << endl;
}
fil.close();
}
int read(int ch, int find = 0)
{
clrscr();
result(ch);
fil.open("database.dat", ios::binary);
fil.seekg(pos[ch], ios::beg);
char entry[20];
fil >> entry;
cout << setw(20) << "Date" << setw(10) << "High" << setw(10) << "Low" << setw(10) << "Close" << endl;
while (entry[0] != '%')
{
if (find == 1)
{
if (strcmp(entry, date))
return(fil.tellg() - 11);
else
continue;
}
cout << setw(20) << entry;
fil >> entry;
cout << setw(10) << entry;
fil >> entry;
cout << setw(10) << entry;
fil >> entry;
cout << setw(10) << entry << endl;
fil >> entry;
delay(500);
}
fil.close();
getch();
clrscr();
companyscreen(ch);
}
void edit(int ch)
{
cout << "Enter date of data to be edited";
gets(date);
write(2, read(ch, 1));
}
void companyscreen(int ch)
{
int ch1;
askagain:
result(ch);
cout << " 1. Add Data" << endl;
cout << " 2. Show history" << endl;
cout << " 3. Edit Data" << endl;
exitt();
ch1 = getch() - 48;
if (ch1 == 1)
write(2);
else if (ch1 == 2)
read(ch);
else if (ch1 == 3)
{
read(ch);
edit(ch);
}
else if (ch1 == 0)
{
cout << " exiting!!" << endl;
exit(500);
}
else if (ch1 == 60)
companychoose();
else
{
cout << " Invalid option chosen" << endl;
getch();
clrscr();
goto askagain;
}
}
void companychoose()
{
char name[20];
int i, ch;
clrscr();
fil.open("database.dat", ios::binary);
askagain:
fil.seekg(0, ios::beg);
cout << " Choose Company:";
cout << endl;
i = 1;
while (!fil.eof())
{
fil >> name;
if (name[0] == '%')
{
name[0] = ' ';
pos[i] = fil.tellg();
cout << setw(10) << i << "." << name << endl;
i++;
}
}
fil.close();
exitt();
ch = getch() - 48;
if (ch == 0)
exit(0);
else if (ch == 60)
options();
else if (ch>i)
{
cout << "Invalid choice" << endl;
getch();
clrscr();
goto askagain;
}
clrscr();
companyscreen(ch);
}
void options()
{
int ch;
clrscr();
askagain:
cout << endl << endl;
cout << " 1. Add company" << endl;
cout << " 2. Choose company" << endl;
exitt(1);
ch = getch() - 48;
if (ch == 1)
write(1);
else if (ch == 2)
companychoose();
else if (ch == 0)
{
cout << setw(10) << " Exiting!!";
exit(500);
}
else
{
cout << setw(10) << " Invalid choice chosen" << endl;
getch();
clrscr();
goto askagain;
}
}
void main()
{
clrscr();
textbackground(MAGENTA);
textcolor(WHITE);
clrscr();
options();
getch();
}
pls note that program is yet not fully complete so some features dont work.
i don't know how to include dat file data nor screenshot here.
i don't use visual c++ cause my pc is slow.
i don't use codeblocks cause i dont know how to use it. above code give hundreds of error even after adding "using namespace std;"
pls help me solve it. if you need anything else then ask me. thanks
I get different answer (…) in function result(int) then what I get from running .exe file created.
When you execute your program from the IDE a different working directory is used so different files are seemingly present/missing. Usually the working directory is configurable.
By the way, the goto is not needed. Really, it is not.

C++ Hangman Code won't work correctly

my code compiles but it doesn't allow the user to guess the word correctly and when it displays the correct word, it is just a string of nonsense.
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int instructions();
void manual();
void file(char*);
int letterFill(char, char*, char*);
void Unknown(char*, char*);
const int MAX_LENGTH = 10;
void main()
{
bool done = false;
char word[MAX_LENGTH];
char unknown[MAX_LENGTH];
char letter;
char name[MAX_LENGTH];
int wrong_guesses = 0;
int MAX_TRIES;
char ans;
while (!done)
{
switch (instructions())
{
case 1:
{
manual();
break;
}
case 2:
{
file(word);
break;
}
}
cout << "WHAT IS THE NUMBER OF GUESSES ALLOWED?: " << endl;
cin >> MAX_TRIES;
Unknown(word, unknown);
cout << endl << endl << "HANGMAN";
cout << endl << endl << "Each letter is represented by a star." << endl;
cout << "You have " << MAX_TRIES << " tries left.";
cout << "ENTER GUESS WHEN READY: ";
cin >> letter;
while (letter != 'N' && letter != 'n')
{
while (wrong_guesses < MAX_TRIES)
{
cout << unknown << endl;
cout << "Guess a letter: " << flush;
cin >> letter;
if (letterFill(letter, word, unknown) == 0)
{
cout << endl << "You got it wrong! You lose a guess" << endl;
wrong_guesses++;
}
else
{
cout << endl << "Pfft, you got lucky" << endl;
}
cout << "Guesses Left: " << MAX_TRIES - wrong_guesses << endl;
if (strcmp(word, unknown) == 0)
{
cout << word << endl;
cout << "You got it!" << endl;
exit(0);
}
cout << "You've been hanged." << endl;
cout << "The word was : " << word << endl;
}
}
cout << "Try again? (y/n): " << flush;
cin >> ans;
if (ans == 'y' || ans == 'Y')
done = true;
else
done = false;
}
system("pause");
}
int instructions()
{
int select = 0;
cout << endl << "HANGMAN" << endl << endl;
cout << " PROGRAM MENU" << endl;
cout << " Select option 1 or 2" << endl << endl;
cout << " 1. INPUT WORD MANUALLY" << endl;
cout << " 2. PLAY AGAINST THE COMPUTER" << endl;
cout << " 3. EXIT PROGRAM BY INPUTING: N or n" << endl << endl;
cin >> select;
return select;
}
void manual()
{
string word;
cout << endl << "INPUT WORD: " << endl;
cin >> word;
return;
}
void file(char *roc)
{
ifstream fin("word.txt");
int x;
int count = 1;
int word;
int i = 0;
srand(time(0));
word = rand() % 20;
while (count < word)
{
fin >> x;
if (x == 0)
{
count++;
}
}
do
{
fin >> x;
roc[i++] = char(x);
} while (x);
return;
}
int letterFill(char guess, char *secretword, char *guessword)
{
int i;
int matches = 0;
for (i = 0; i<MAX_LENGTH; i++)
{
if (secretword[i] == 0)
{
break;
}
if (guess == guessword[i])
{
return 0;
}
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
void Unknown(char *word, char *unknown)
{
int i;
int length = strlen(word);
for (i = 0; i<length; i++)
{
unknown[i] = '*';
}
unknown[i] = 0;
}
Again
my code compiles but it doesn't allow the user to guess the word correctly and when it displays the correct word, it is just a string of nonsense.