why is loop stuck - c++

Only problems remaining now are that my choice while loop is infinite since the break statements dont seem to be breaking out of the loop at all, so the program doesn't read the answer loop. Also, "Invalid Entry" displays every other incorrect input instead of displaying everytime an invalid character is entered
#include <iostream>
#include <cctype>
using namespace std;
int getAges(int age, const int SIZE);
char getChoice();
void displayInOrder(int numbers[], const int SIZE, char choice);
void displayInReverse(int numbers[], const int SIZE, char choice);
int main()
{
const int SIZE = 5;
int numbers[SIZE] = { 1, 2 ,3 ,4, 5 };
char answer = 0;
int age = 0;
char choice = 0;
while (choice = getChoice())
{
if (toupper(choice) == 'O')
{
displayInOrder(numbers, SIZE, choice);
break;
}
else if (toupper(choice) == 'R')
{
displayInReverse(numbers, SIZE, choice);
break;
}
else
{
cout << "Invalid entry! - Must be O or R\n\n";
break;
}
}
while (toupper(answer) == 'Y')
{
system("cls");
age = getAges(age, SIZE);
choice = getChoice();
displayInOrder(numbers, SIZE, choice);
displayInReverse(numbers, SIZE, choice);
cout << "Run program again (Y or N)? ";
cin >> answer;
if (toupper(answer) == 'N')
{
exit();
}
}
return 0;
}
int getAges(int age, const int SIZE)
{
cout << "Enter " << SIZE << " ages: \n\n";
cin >> age;
cout << endl;
cin >> age;
cout << endl;
cin >> age;
cout << endl;
cin >> age;
cout << endl;
cin >> age;
cout << endl;
return age;
}
char getChoice()
{
char choice;
cout << "How do you want to see the ages displayed? \n\n Enter O for In Order, or R for In Reverse.\n\n";
cin >> choice;
return choice;
}
void displayInOrder(int numbers[], const int SIZE, char answer)
{
cout << "Here are the ages in order: \n\n";
for (int i = 0; i < SIZE; i++)
{
cout << numbers[i] << endl;
}
}
void displayInReverse(int numbers[], const int SIZE, char answer)
{
cout << "Here are the ages in reverse order: \n\n";
for (int i = SIZE - 1; i >= 0; i--)
{
cout << numbers[i] << endl;
}
}

1."Invalid Entry" displays every other incorrect input: you have a getChoice() call inside the while loop, here:
else
{
cout << "Invalid entry! - Must be O or R\n\n";
choice = getChoice();
}
which is followed by a getChoice() call in the
while (choice = getChoice())
Hence the previous getChoice() is not processed.
2."I'm not sure how to close the program if the user enters N for answer", You should think about if you can ever reach the second while loop and where you are setting the answer variable or basically where you are taking the user input for ending the program? You should see if it can it be taken care of in the first while loop ?

Related

how to find a specific target in the array c++

The search function should get a value from the user to search for it, if the value is found, then it should print it out, and if not found it should print not found.
However, in my code every time I write the number that is in the array is gives me the false option although it is in the array stored.
`
#include <iostream>
using namespace std;
const int size = 100;
int partsmenu(int menu_option);
void readparts(char part_number[][10], double price[], char classification[], int& number_of_parts);
int search(char part_number[][10], char search_target[], int number_of_parts, double price[], char classification []);
void display_parts(char part_number[][10], double price[], char classification[], int& number_of_parts);
int main()
{
const int size = 100;
int menu_option=0, option, displaysearch;
char part_number[size][10];
double price[size];
char classification[size];
int number_of_parts = 0;
char search_target[size];
//using switch statment to make it look like a menu option
do {
switch (option = partsmenu(menu_option))
{
case 1:
readparts(part_number, price, classification, number_of_parts);
break;
case 2:
display_parts(part_number, price, classification, number_of_parts);
break;
case 3:
displaysearch = search(part_number, search_target, number_of_parts, price, classification);
break;
case 4:
break;
default:
cout << "Not valid..." << endl;
break;
}
cout << endl;
} while (option != 4);
return 0;
}
int partsmenu(int menu_option)
{
cout <<"1) Enter new part number\n2) View all part numbers\n3) Search for part\n4) Exit\n\nEnter an option: ";
cin >> menu_option;
return menu_option;
}
void readparts(char part_number[][10], double price[], char classification[], int& number_of_parts)
{
// using for loop to store part number, price, and classification in the array
int number;
cout << "Enter number of parts to add:";
cin >> number;
cout << endl;
int i;
for (i = number_of_parts; i < (number_of_parts+number); i++)
{
cout << "Enter part number: ";
cin >> part_number[i];
cout << "Enter price: ";
cin >> price[i];
cout << "Enter classificarion: ";
cin >> classification[i];
//using if statment to check for the classificarion
if (classification[i] == 'A' || classification[i] == 'B' || classification[i] == 'C')
cout << "";
else
{
cout << "Invalid case..." << endl;
cout << "Enter Valid class [A, B, C]: ";
cin >> classification[i];
cout << endl;
}
cout << endl;
}
number_of_parts = i;
}
int search(char part_number[][10], char search_target[], int number_of_parts, double price[], char classification[])
{
//searching for specific data
bool found = false;
int value;
cout << "Enter part number: ";
for (int j = 0; j < number_of_parts; j++)
{
cin >> search_target;
for (int i = 0; i < number_of_parts && found == false; i++)
{
if (part_number[i] == search_target)
found = true;
value = i;
}
}
if (found == true)
{
for (int i = 0; i < number_of_parts; i++)
{
cout << "Part ID\t\tPrice\t\tClass" << endl;
cout << " --------------------------------------------" << endl;
cout << part_number[value] << "\t\t" <<price[value]<< "\t\t" <<classification[value]<< endl;
}
}
else
{
cout << "No parts found..." << endl;
value = -1;
}
return value;
}
void display_parts(char part_number[][10], double price[], char classification[], int& number_of_parts)
{
// displaying the data
cout << "Part ID\t\tPrice\t\tClass" << endl;
cout << "--------------------------------------------" << endl;
for (int i = 0; i < number_of_parts; i++)
{
cout << part_number[i] << "\t\t" << price[i] << "\t\t" << classification[i] << endl;
}
cout << endl;
}
`
I am trying to find what is wrong with code but could not find any fault with it. Everything else works fine.
You compare C strings with strcmp not ==. Like this
if (strcmp(part_number[i], search_target) == 0)
If you use == then all you are comparing is the addresses of the strings, the addresses of two strings can be different even if the string contents are the same.
strcmp compares the actual characters in the strings, not the addresses. It returns 0 if the two strings are the same.

Having an issue with my switch menu in a while loop

So I am working on a switch inside of a while loop. And it is not behaving properly. When I select 'l' and load the file it lets me select again, then when I try and press 'p' to print it, it just keeps looping over the selection prompt. I am pretty sure it is because choice != 'q', but don't know how to fix it.
Thank you for any help.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
//create a struct called Weather
struct Weather {
int month;
int date;
int high;
int avg;
int low;
double precip;
string event;
};
//function prototypes
int loadData(ifstream &file, Weather days[1000]);
void printData(Weather days[1000], int count);
int main() {
// declare variables
Weather days[1000];
ifstream inFile;
string checker;
char choice = '0';
int month = 0, count;
string path;
cout << "Welcome to the weather analyzer!" << endl;
while (choice != 'q') {
cout << "Would you like to (l)oad data, (p)rint data, (s)earch data, (o)rder the data, or (q)uit? ";
cin >> choice;
cout << endl;
switch (choice) {
case 'l':
// promt user for file path
cout << "Please enter the file path: ";
cin >> path;
// open the file
inFile.open(path);
// checks to see if file successfully opened and terminates if not
if (!inFile) {
cout << "Bad Path";
getchar();
getchar();
return 0;
}
loadData(inFile, days);
count = loadData(inFile, days);
break;
case 'p':
printData(days, count);
break;
case 's':
case 'o':
case 'q':
cout << "Good bye!";
break;
default:
cout << "Invalid option";
}
}
// Close file.
inFile.close();
// Pause and exit.
getchar();
getchar();
return 0;
}
//loading function
int loadData(ifstream &inFile, Weather days[1000]) {
string checker;
int month = 0;
int i; //i varaiable keeps track of how many lines there are for the print function
for (i = 0; !inFile.eof(); i++) {
inFile >> days[i].date; // gets date and checks if it is 2017 with if loop
if (days[i].date == 2017) {
getline(inFile, checker);
getline(inFile, checker);
inFile >> days[i].date; //gets correct date value
month++;//increments month counter
}
days[i].month = month;//gets and stores data from file into days
inFile >> days[i].high
>> days[i].avg
>> days[i].low
>> days[i].precip;
getline(inFile, days[i].event);
}
return i; //returns amount of days
}
// printing function
void printData(Weather days[1000], int count) {
for (int i = 0; i < count; i++) {
cout << days[i].month << " "
<< days[i].date << " "
<< days[i].high << " "
<< days[i].avg << " "
<< days[i].low << " "
<< days[i].precip << " "
<< days[i].event << " ";
cout << endl;
}
}
After reading the user input with cin, you probably want to flush the cin buffer:
cin.clear();
cin.ignore(INT_MAX);

Why does my program skip the first function? (Beginner C++)

I don't understand why getAges is being skipped.
#include <iostream>
#include <cctype>
using namespace std;
int getAges(int age, const int SIZE);
char getChoice();
void displayInOrder(int numbers[], const int SIZE, char choice);
void displayInReverse(int numbers[], const int SIZE, char choice);
int main()
{
const int SIZE = 5;
int numbers[SIZE] = { 1, 2 ,3 ,4, 5 };
char answer;
int age;
char choice;
if (toupper(choice) == 'O')
{
displayInOrder(numbers, SIZE, choice);
}
else if (toupper(choice) == 'R')
{
displayInReverse(numbers, SIZE, choice);
}
else
{
cout << "Invalid entry! - Must be O or R\n\n";
}
if (toupper(answer) == 'Y')
{
system("cls");
age = getAges(age, SIZE);
choice = getChoice();
displayInOrder(numbers, SIZE, choice);
displayInReverse(numbers, SIZE, choice);
cout << "Run program again (Y or N)? ";
cin >> answer;
break;
}
else if (toupper(answer) == 'N')
{
return 0;
}
return 0;
}
int getAges(int age, const int SIZE)
{
cout << "Enter " << SIZE << " ages: \n\n";
cin >> age;
cout << endl;
cin >> age;
cout << endl;
cin >> age;
cout << endl;
cin >> age;
cout << endl;
cin >> age;
cout << endl;
return age;
}
char getChoice()
{
char choice;
cout << "How do you want to see the ages displayed? \n\n Enter O for In Order, or R for In Reverse.\n\n";
cin >> choice;
return choice;
}
void displayInOrder(int numbers[], const int SIZE, char answer)
{
cout << "Here are the ages in order: \n\n";
for (int i = 0; i < SIZE; i++)
{
cout << numbers[i] << endl;
}
}
void displayInReverse(int numbers[], const int SIZE, char answer)
{
cout << "Here are the ages in reverse order: \n\n";
for (int i = SIZE - 1; i >= 0; i--)
{
cout << numbers[i] << endl;
}
}
I started working on this before the OP updated the title to their original question about "while loops & break statements". However at the time I came across this question the OP had originally removed the while loops. I was looking over the provided functions to get an idea of what the OP was trying to do and this is what I have come up with.
First: while loops are exactly what you want here, but you want a specific type of while loop, a do-while loop in this case.
Next: There is no need for break statements if you know how to structure your do-while loop correctly.
Finally: I made some modifications to the OP's existing functions by changing or removing unnecessary parameter(s), return type(s) & code duplication. I removed a function that was no longer needed. I changed the output formatting of the messages to display a clean looking program. I also removed a bad practice of having using namespace std; in the global scope.
I did this to demonstrate to the OP how a while loop can be constructed without the need of break statements and that they were originally on the right track but needed a little bit of assistance to get on their way.
Here is the source code to a working program on what I think the OP was aiming to do.
#include <iostream>
#include <cctype>
void getAge( int& age );
void displayInOrder( int numbers[], const int SIZE);
void displayInReverse( int numbers[], const int SIZE );
int main() {
const int SIZE = 5;
int numbers[SIZE] = { 0 };
char answer = '\0';
int age = 0;
char choice = '\0';
std::cout << "========================================================================\n"
<< "This program will have the user enter in "
<< SIZE
<< " Ages. \nThen ask the user in which order to display the list of ages.\n"
<< "Finally the program will ask the user if they want to continue or not.\n"
<< "========================================================================\n\n";
do {
for ( int i = 0; i < SIZE; i++ ) {
getAge( age );
numbers[i] = age;
}
std::cout << "\nPlease enter 'O' for ordered or 'R' for reversed list.\n";
std::cin >> choice;
if ( toupper( choice ) == 'O' ) {
displayInOrder( numbers, SIZE );
}
if ( toupper( choice ) == 'R' ) {
displayInReverse( numbers, SIZE );
}
std::cout << "\nDo you want to run program again (Y/N)?";
std::cin >> answer;
} while ( toupper( answer ) == 'Y' );
return 0;
}
void getAge( int& age ) {
int temp;
std::cout << "Enter an age: \n";
std::cin >> temp;
age = temp;
}
void displayInOrder( int numbers[], const int SIZE ) {
std::cout << "\nHere are the ages in order: \n";
for ( int i = 0; i < SIZE; i++ ) {
std::cout << numbers[i] << std::endl;
}
}
void displayInReverse( int numbers[], const int SIZE ) {
std::cout << "\nHere are the ages in reverse order: \n";
for ( int i = SIZE - 1; i >= 0; i-- ) {
std::cout << numbers[i] << std::endl;
}
}

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)
// ^^^^^^^^^^

Using Template Function for int, float and char for finding the lowest value

Prompt:
Convert the functions theSmallest() and fill_array()to template functions.
Test your template functions with 3 different data types: int, float and char.
Problem:
When I load this into the compiler, there are no errors. It works for INT and Float; however when I run the index_of_smallest function for the character option... it doesn't like it. When I actually check with a "cout" what the value for min is during a char call, it is giving me "-10002e89..". That value never changes so in the for loop the index_of_min is always 0 so the compiler thinks that the smallest character is always at the index of 0.
CODE:
#include <iostream>
#include <cstdlib>
using namespace std;
const int DECLARED_SIZE = 20;
template <class BaseType>
int index_of_smallest(BaseType a[], int size, int& number_used)
{
BaseType min = a[0];
int index_of_min = 0;
cout << index_of_min<< endl;
for (int index = 1 ; index < number_used; index++)
{
if (a[index] < min)
{
min = a[index];
index_of_min = index;
}
}
cout << index_of_min<< endl;
return index_of_min;
}
template <class BaseType>
void fill_array(BaseType a[], int size, int& number_used)
{
int index = 0;
BaseType ans;
cout << "Enter up to " << size << " of the Data Type selected.\n"
<< "Mark the end of a list of numbers with a negative number.\n"
<< "Mark the end of a list of characters with the negative symbol \"-\". \n"
<< "Separate each input with a space. \n" << endl;
cin >> ans;
while (ans >= 0 && ans != '-')
{
a[index] = ans;
index++;
cin >> ans;
}
number_used = index;
}
void menu()
{
int number_used, choice, smallest, result;
int start_index = 0;
char menuLoop = 'Y';
while (toupper(menuLoop) == 'Y')
{
cout << "Please select a type of Input: \n"
<< "1. Integers (1, 2, 3, 4, etc..)\n"
<< "2. Decimals (1.0, 2.0, 3.0. 4.0, etc..)\n"
<< "3. Characters ( a, b, c, d, etc...)\n\n\n";
cin >> choice;
switch (choice)
{
case 1:
int arrI[DECLARED_SIZE];
system ("CLS");
fill_array(arrI, DECLARED_SIZE, number_used);
smallest = index_of_smallest(arrI, start_index, number_used);
cout << "The smallest integer is: " << arrI[smallest] << " ";
break;
case 2:
float arrF[DECLARED_SIZE];
system ("CLS");
fill_array(arrF, DECLARED_SIZE, number_used);
smallest = index_of_smallest(arrF, start_index, number_used);
cout << "The smallest float is: "<< arrF[smallest] << " ";
break;
case 3:
char arrC[DECLARED_SIZE];
system ("CLS");
fill_array(arrC, DECLARED_SIZE, number_used);
smallest = index_of_smallest(arrF, start_index, result);
cout << "The smallest character is: " << arrC[smallest] <<" \n";
break;
default:
cout << "That is an invalid choice! Exiting Program";
}
cout << "\n\nWould you like to return to the main menu? (Y/N): ";
cin >> menuLoop;
while(toupper(menuLoop) != 'Y' && toupper(menuLoop) != 'N')
{
cout << "Invalid Choice: Please type (Y/N): ";
cin >> menuLoop;
}
system("CLS");
}
}
int main()
{
using namespace std;
menu();
system("PAUSE");
}
Your error is here:
smallest = index_of_smallest(arrF, start_index, result);
Which should be:
smallest = index_of_smallest(arrC, start_index, number_used);
You were using the wrong array, and you were passing result (which was never initialized) instead of number_used.