I having problem with my searching function C++ - c++

i'am begginner in C++, i want to ask question about what's wrong about my coding, when i doing my work then found out problem i can't solve in searching function, there's no error notice or anything from visual studio but my function still didn't work,
sorry for my bad english.
here is my full code
#include <iostream>
#include <cstdlib>
#define MAX 5
using namespace std;
struct College {
int nim;
string name;
}mahasiswa;
struct queue {
College college[MAX];
int start,end = - 1;
}antrean;
void init() {
antrean.start= -1;
antrean.end = -1;
}
bool full() {
if (antrean.end== MAX - 1) {
return true;
}
else {
return false;
}
}
bool empty() {
if (antrean.end== -1) {
return true;
}
else {
return false;
}
}
void tampilData() {
int i;
if (!empty()) {
for (i = antrean.start; i < antrean.end; i++)
{
cout<<antrean.college[i].nim<<" | ";
cout << antrean.college[i].name << " | ";
cout << "\n";
}
}
cout<<"\n";
}
void inQueue() {
tampilData();
int elemennim;
string elemenname;
if (!full()) {
cout << "Input your NIM : ";
cin >> elemennim;
cout << "\n";
cout << "Input your name: ";
cin >> elemenname;
cout << "\n";
cout << "Succefully\n";
antrean.college[antrean.end].nim = elemennim;
antrean.college[antrean.end].name = elemenname;
antrean.end++;
}
else
{
cout << "Queue Penuh\n";
}
}
and this the error function i cant solve,
void searching(int key) {
for (int i = 0; i <= antrean.start; i++)
{
if (key == antrean.college[i].nim) {
cout << "Element found in index " << i;
}
else
{
cout << "Element not found\n";
}
cout << antrean.college[i].nim;
}
}
int main() {
int choice, elemen;
init();
cout << "Demo Queue dengan Linear Array" << endl;
do {
tampilData();
cout << "\nMenu Utama\n";
cout << "==============\n";
cout << "[1] Init \n[2] InQueue \n[3] Searching \n[4] out \n";
cout << "==============\n";
cout << "\nMasukkan pilihan: "; cin >> choice;
cout << "==============\n";
switch (choice) {
case 1: init(); break;
case 2: inQueue(); break;
case 3: cout << "masukkan NIM yang ingin dicari \n";
cin >> choice;
searching(choice); break;
} while (choice != 0);
return 0;
}
please help me, thank you very much

Related

static stack function does not 1) take input of 1st element 2)recognise old element when new element is added

There is an array of nodes(a structure) and is used as a
stack.
it has 3 functions
to add new elements (push)
to delete elements(pop)
to display(display)
problems:
does not save the first input
when a new input is added, it replaces the previous node with the new input.
please help me identify where i have gone wrong
#include <iostream>
#include<conio.h>
#include<process.h>
struct node
{
int x, y;
};
int top = -1;
class stack
{
node s[30];
public:
void push();
void pop();
void display();
};
void stack::push()
{
if (top < 29)
{
cout << "enter elements" << endl;
int a, b;
cin >> a >> b;
top = top + 1;
s[top].x = a;
s[top].y = b;
}
else
{
cout << "OVERFLOW" << endl;
}
}
void stack::pop()
{
node temp;
temp = s[top];
top--;
cout << "element" << temp.x << "&" << temp.y << " has been deleted" << endl;
}
void stack::display()
{
for (int i = 0; i < top; i++)
{
cout << s[top].x << "&" << s[top].y << endl;
}
}
void main()
{
clrscr();
stack sup;
int choice = 1;
do
{
cout << "1.add" << endl << "2.delete" << endl << "3.display" << endl;
int c;
cin >> c;
switch (c)
{
case 1:
sup.push();
sup.display();
break;
case 2:
sup.pop();
sup.display();
break;
case 3:
sup.display();
break;
default:
cout << "error in switch case" << endl;
}
cout << "enter 1 to perform more operations" << endl;
cin >> choice;
} while (choice == 1);
getch();
}
You delete and recreate you stack (sup) every iteration of your loop. The declaration should be before the do.

vector name storage list program suddenly not displaying list

Novice C++ user trying to practice program building. The point of this program is just simple name storage with vectors.
My previous program https://pastebin.com/MG1hHzgK works perfectly fine for just adding first names.
This upgraded version is supposed to have an input of First Last names then it is converted into Last, First name before being added to the list.
My problem is that after I input names, they arent added to the list. The differences between my previous program and current one are all in the function addNames and to me it looks correct when its obviously not.
Any hints or help is greatly appreciated.
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Prototypes
string addNames(vector <string>& nameList);
string removeName(vector <string>& nameList);
int findName (vector <string>& nameList);
void showList(vector <string>& nameList);
void commandList(vector <string>& nameList);
void inputCall(vector <string>& nameList);
void sortList(vector <string>& nameList);
int main()
{
vector <string> nameList;
commandList(nameList);
}
void commandList(vector <string>& nameList)
{
cout << "\nPress any key to continue..." << endl;
getch();
system("cls");
cout << "Enter a Command " << endl;
cout << "<A> - Add names to the list" << endl;
cout << "<R> - Remove a name from the list" << endl;
cout << "<F> - Search for a name on the list" << endl;
cout << "<L> - Show current state of the list" << endl;
cout << "<S> - Sort the list" << endl;
cout << "<Q> - Ends the program" << endl;
inputCall(nameList);
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
string addNames(vector <string>& nameList)
{
string input;
int pos = input.find(' ');
nameList.clear();
for (;true;)
{
cout << endl;
cout << "Enter a Name or 'Stop' to end name entry: " << endl;
getline(cin, input);
if (input == "Stop" || input == "stop"){
commandList(nameList);
} else if(pos != -1) {
string first = input.substr(0, pos);
string last = input.substr(pos + 1);
input = last + "," + first;
nameList.push_back(input);
commandList(nameList);
}
}
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
string removeName(vector <string>& nameList)
{
string x;
cout << endl;
cout << "Enter the name to remove: " << endl;
cin >> x;
for (int i=0; i < nameList.size(); ++i) {
if (nameList[i]== x) nameList[i]="";
}
commandList(nameList);
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
int findName (vector <string>& nameList)
{
string target;
int i, x=0;
int p=0;
cout << endl;
cout << "Enter a name to search for: " << endl;
cin >> target;
if (target == "Quit" || target == "quit") {exit(0);
}
for (int i=0; i < nameList.size(); i++)
{
if (nameList[i] == target)
{
cout << endl;
cout << "The entered name is listed as #" << p+1 << '.' << endl;
commandList(nameList);
return p;
}
if (nameList[i] == "") {
p--;
}
p++;
}
cout << endl;
cout << "Name not found!" << endl;
commandList(nameList);
return -1;
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
void showList(vector <string>& nameList)
{
cout << endl;
cout << "The current state of the list is: " <<endl;
for (int i=0; i<nameList.size(); i++)
if(nameList[i] !="")
cout << nameList[i] << endl;
commandList(nameList);
}
void sortList(vector <string>& nameList)
{
string temp;
for (int i=0; i < nameList.size()-1; i++)
{
for (int j=0; j < (nameList.size()-i-1); j++)
{
if (nameList[j] > nameList[j+1])
{
temp = nameList[j];
nameList[j] = nameList[j+1];
nameList[j+1] = temp;
}
}
}
cout << endl;
cout << "The list has been sorted alphabetically." << endl;
commandList(nameList);
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------
void inputCall(vector <string>& nameList) // Function to complement the menu for switch casing
{
bool running = true;
char input;
do {
input = getch();
switch(input)
{
case 'a': addNames(nameList);break;
case 'A': addNames(nameList);break;
case 's': sortList(nameList);break;
case 'S': sortList(nameList);break;
case 'l': showList(nameList);break;
case 'L': showList(nameList);break;
case 'f': findName(nameList);break;
case 'F': findName(nameList);break;
case 'r': removeName(nameList);break;
case 'R': removeName(nameList);break;
case 'q': exit(0);break;
case 'Q': exit(0);break;
default : cout << "Unknown Command: Enter a command from the menu." << endl; continue;
}
} while (running);
}
if you insert
pos = input.find(' '); in else { } just above ( if(pos != -1) )
Your code will work

What is a deleted function, and why only my functions that I pass files into are considered deleted? [duplicate]

This question already has answers here:
Using fstream Object as a Function Parameter
(3 answers)
Closed 6 years ago.
#include <bits/stdc++.h>
using namespace std;
class contact {
private:
vector< pair<string, int> > contact_info;
public:
void add_contact(string contact_name, int contact_number) {
contact_info.push_back(make_pair(contact_name, contact_number));
sort(contact_info.begin(),contact_info.end());
}
void edit_contact(string contact_name) {
int found_at;
for (unsigned int i =0; i < contact_info.size(); i++) {
if (contact_info[i].first == contact_name) {
found_at = i;
}
}
if (contact_info[found_at +1].first == contact_name) {
int choice;
int counter = found_at;
int index = 1;
while (contact_info[counter].first == contact_name) {
cout << index << ". " << contact_info[counter].first << " " << contact_info[counter].second;
counter++;
index++;
}
cout << "Choose any please: ";
cin >> choice;
found_at = found_at - (choice - 1);
}
cout << "Enter the new number: ";
cin >> contact_info[found_at].second;
}
void show_all() {
for (unsigned int i =0; i < contact_info.size(); i++) {
cout << contact_info[i].first << " " << contact_info[i].second << endl;
}
}
void delete_contact(string contact_name) {
int found_at;
for (unsigned int i =0; i < contact_info.size(); i++) {
if (contact_info[i].first == contact_name) {
found_at = i;
}
}
if (contact_info[found_at +1].first == contact_name) {
int choice;
int counter = found_at;
int index = 1;
while (contact_info[counter].first == contact_name) {
cout << index << ". " << contact_info[counter].first << " " << contact_info[counter].second;
counter++;
index++;
}
cout << "Choose any please: ";
cin >> choice;
found_at = found_at - (choice - 1);
}
contact_info.erase(contact_info.begin()+found_at);
}
void writeFile(ofstream contact_file) {
for (unsigned int i =0; i < contact_info.size(); i++) {
contact_file << contact_info[i].first << " " << contact_info[i].second << endl;
}
}
void readFile(ifstream contact_file) {
string input;
while (!contact_file.eof()) {
contact_file >> input;
size_t pos = input.find(" ");
string name = input.substr(0,pos);
string number_str = input.substr(pos);
int number = stoi(number_str) ;
contact_info.push_back(make_pair(name,number));
}
}
};
int main()
{
int choice;
ifstream contacts_file_read;
contacts_file_read.open("contacts.txt");
ofstream contacts_file_write;
contacts_file_write.open("contacts.txt");
bool in_prog = true;
contact contacts;
string name;
int number;
while (in_prog) {
cout << "1. Add contacts" << endl
<< "2. Edit contact" << endl
<< "3. Delete contact" << endl
<< "4. Show all" << endl
<< "5. exit" << endl;
cout << "Your choice: ";
cin >> choice;
contacts.readFile(contacts_file_read);
if (choice == 1) {
cout << "Enter name & number separated by a space: ";
cin >> name >> number;
contacts.add_contact(name, number);
} else if (choice == 2) {
cout << "Enter name of contacts to be edited: ";
cin >> name;
contacts.edit_contact(name);
} else if (choice == 3) {
cout << "Enter name of contact to be deleted: ";
cin >> name;
contacts.delete_contact(name);
} else if (choice == 4) {
contacts.show_all();
} else if(choice == 5) {
contacts.writeFile(contacts_file_write);
} else {
cout << "Wrong choice" << endl;
}
}
return 0;
}
So, I was asked in my programming class to make a phone book application in C++ using only objects, so this is my attempt at it.
All functions are good, I did recompile the program after finishing each function at it gave me 0 errors, however whenever I try to call writeFile or readFile function that were previously working fine, now the compiler gave me an error of "error: use of deleted functions... "
I don't know what are deleted functions and why only functions that take file objects as an argument are treated as such.
Can anyone please help?
Thanks.
Objects of type std::ifstream are not copyable -- indeed, the object represents the unique handle of an open file, and it would be difficult to conceptualize what it would mean to copy such unique responsibility.
Indeed, this inability to copy an object is encoded by making the copy constructor deleted, which causes the error that you see when you do attempt to copy it.
Your code should pass the original ifstream, not a copy (by taking a reference parameter):
void readFile(ifstream & contact_file)
// ^^^^^^^^^^

program crashes after when trying to load the csv file

me again. I have a problem. I tried to run this code and when I compile it, the compiler shows no errors, but after running it, the program crashes. HELP!!!
What the program should do:
It searches trough the data of deseases and desease codes of the WHO ( World Health Organisation). Two csv files are given (english and german version) and you have a choice to search in english and in german. I think that the program chrashes when it tries to load the char*-s from the csv file.
It's driving me crazy.
Here is also the link where you can find the whole project:
LINK
Code:
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <list>
#include <algorithm>
using namespace std;
enum KEY {code, disease} key;
enum LANGUAGE {english, deutsch} language;
char* buffer;
struct lst
{
char *_code;
char *_disease;
};
streamsize _buffer = 10000;
bool isPartOf(char* word, char* sentence)
{
unsigned int i=0;
unsigned int j=0;
for(;i < strlen(sentence); i++)
{
if(sentence[i] == word[j])
{
j++;
}
}
if(strlen(word) == j)
return true;
else
return false;
}
void printList(list<lst>* LST)
{
for(list<lst>::iterator i= LST->begin(); i != LST->end(); i++)
{
cout << i->_code << '\t' << i->_disease << endl;
}
}
list<lst>* makeList(char* fileName)
{
int i,j;
fstream ICDcsv;
ICDcsv.open(fileName);
list<lst>* new_list = new list<lst>;
if(ICDcsv.is_open())
{
while(ICDcsv)
{
lst* x = new lst;
ICDcsv.getline(buffer,_buffer);
i = 0;
j = 0;
while (buffer[i] != ';')
{
x->_code[i] = buffer[i];
i++;
}
i++;
while (buffer[i] != ';')
{
x->_disease[j] = buffer[i];
i++;
j++;
}
(*new_list).push_back(*x);
}
ICDcsv.close();
}
else{cerr << "Error: file error" << endl;}
return new_list;
}
list<lst>* listSearch(list<lst> *LST,char* wrd,KEY key)
{
switch(key)
{
case code:
for(list<lst>::iterator i = LST->begin(); i != LST->end(); i++)
{
if(!isPartOf(wrd, ( *i )._code))
{
delete [] i->_code;
delete [] i->_disease;
delete &(*i);
}
} //i->
break;
case disease:
for(list<lst>::iterator i = LST->begin(); i != LST->end(); i++)
{
if(!isPartOf(wrd, ( *i )._disease))
{
delete [] i->_code;
delete [] i->_disease;
delete &(*i);
}
}
break;
}
return LST;
}
int main()
{
int choice;
int program_end=1;
char* _file;
char* Search;
cout << "World Health Institution (WHO)/Weltgesundheitsorganisation" << endl;
cout << "International Statistical Classification of Diseases and Related Healt Problems (ICD)/Internationale statistische Klassifikation der Krankheiten und verwandter Gesundheitsprobleme" << endl;
cout << "1 english" << endl;
cout << "2 deutsch" << endl;
cout << "your choice/Ihre Auswahl: ";
cin >> choice;
cin.clear();
if(choice == 1)
language = english;
else
language = deutsch;
switch (language)
{
case english:
{
_file = "care_icd10_en.csv";
break;
}
case deutsch:
{
_file = "care_icd10_de.csv";
break;
}
}
cout << "0 end/Ende" << endl;
cout << "1 search for ICD code (e.g. K52.9)/Suche nach ICD Kode (Beispiel K52.9)" << endl;
cout << "2 search for desease (e.g. Ebola)/Suche nach Krankheit (Beispiel Ebola)" << endl;
cout << "your choice/Ihre Auswahl: ";
cin >> program_end;
cin.clear();
switch(program_end)
{
case 0: break;
case 1:
key = code;
cout << "to search for ICD code/zu suchender ICD Kode: ";
break;
case 2:
key = disease;
cout << "to search for deseade/zu suchende Krankheit: ";
break;
}
if(program_end != 0)
{
cin >> Search;
list<lst>* test = makeList(_file);
list<lst>* test2 = listSearch(test,Search,key);
printList(test);
}
return 0;
}

Phone book project error.There are no errors found in compiler but it exits when it runs have been trying for very long [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<cstring>
using namespace std;
int i, j;
struct info
{
char name[20];
int number;
};
void addcontact(info contactlist[]);
void editcontact(info contactlist[]);
void deletecontact(info contactlist[]);
void showallcontact(info contactlist[]);
void exit(info contactlist[]);
void menue(void);
void main()
{
menue();
}
void menue(void)
{
cout << "_______________________n";
cout << " PHONE BOOK n";
cout << "_______________________n";
cout << "1-Add Contactn";
cout << "2-Edit Contactn";
cout << "3-Delete Contactn";
cout << "4-Show All Contactsn";
cout << "5-Exitn";
int option;
cin >> option;
if (option == 1)
{
void addcontact(info contactlist[]);
}
else if (option == 2)
{
void editcontact(info contactlist[]);
}
else if (option == 3)
{
void deletecontact(info contactlist[]);
}
else if (option == 4)
{
void showallcontact(info contactlist[]);
}
else if (option == 5)
{
void exit(info contactlist[]);
}
}
void addcontact(info contactlist[])
{
i = 0;
system("CLS");
cout << "Welcome to Add contact sectionn";
cout << "Enter namen";
cin >> contactlist[i].name;
cout << "Enter numbern";
cin >> contactlist[i].number;
cout << "Contact Addedn";
i = i + 1;
if (i == 19)
{
cout << "Contact limit reachedn";
}
menue();
}
void editcontact(info contactlist[])
{
int flag = 0;
int k;
char name[20];
system("CLS");
cout << "Welcome to Edit contact sectionn";
cout << "Enter name to editn";
cin >> name;
for (int k = 0; k < 20; k++)
{
if (strcmp(name, contactlist[k].name) == 0);
{
flag = 1;
break;
}
}
if (flag == 1)
{
cout << "Enter a new namen";
cin >> contactlist[k].name;
cout << "Contact Editedn";
menue();
}
else if (flag != 1)
{
cout << "No record foundn";
}
}
void deletecontact(info contactlist[])
{
char name[20];
cout << "Enter name of contact to be deleted" << endl;
cin >> name;
for (int i = 0; i < 20; i++)
{
if (strcmp(name, contactlist[i].name) == 0)
{
strcpy(contactlist[i].name, " ");
cout << "contact deletedn" << endl;
menue();
}
}
}
void showallcontacts(info contactlist[])
{
int l;
cout << "detailsn";
for (l = 0; l < 20; l++)
{
cout << contactlist[l].name << endl;
cout << contactlist[l].number << endl;
menue();
}
}
void exit()
{
exit(0);
}
Your code does not make any sense. There are many problems with what you have written
I'm not going to go through them all, but the most glaring is
if (option == 1) {
void addcontact(info contactlist[]);
}
This is not how a function is called. Instead, it should look like
if (option == 1)
{
addcontact(x);
}
where X is an "info" type of object, which you don't have defined.
I'd strongly suggest finding some basic programming tutorials to get a better familiarity with what you are doing.