Logical Error. Not Returning

G

GRoll21

The problem starts during my while loop. It checks to see if they hit x
or X. If they did, then that means the user wants to exit. Then should
go on and hit the return statement. It just quits. When I hit x, it
pauses for a second then just goes to the next line on the cmd prompt.
My other returns work just fine though. I'm sure its just a stupid
logical error somewhere that I havn't caught. If anyone can point out
any tips that or whats wrong with it that would be great. Thanks again
to all!

int editBook(int a)
{

///////used to hold new values///
char xisbn[14];
char xtitle[51];
char xauthor[51];
char xpublisher[31];
char xdate[11];
int xqty;
float xwholeSale;
float xretail;
/////////////////////////////////

char change ='z';
char lookUp[51];
bool found = false;

system("cls");
cout << "\n\t\tEnter the title of the book you wish to edit ";
cin >> lookUp;
system("cls");

for(int index = 0; index < 20 && !found; index++)
{
// toupper
char up1[51], up2[51];
for(int position=0; position < 51; position++)
{
up1[position] = toupper(lookUp[position]);
up2[position] = toupper(bookTitle[index][position]);
}
if (strcmp(up1, up2) == 0)
{
found = true;
bookinfo(isbn[index], bookTitle[index], author[index],
publisher[index], dateAdded[index], qtyOnHand[index], wholesale[index],
retail[index]);
}
}

if(!found)
{
cout << "Book not in Inventory";
return 0;
}


cout <<"\n\nEnter the first letter of the category you would like to
edit or 'x' to exit: ";
cin >> change;

while (change != 'x' || change!='X')
{
if (change == 'I' || change =='i')
{ cin.ignore();
cout << "Enter the new ISBN number: ";
cin.getline(xisbn,14);
strcpy(isbn[index], xisbn);
cout << "The isbn is now " << isbn[index];
cout <<"\n\nEnter the first letter of the category you would like to
edit or 'x' to exit: ";
cin >> change;

}
if (change == 'T' || change =='t')
{
cin.ignore();
cout << "Enter the new Title: ";
cin.getline(xtitle,51);
strcpy(bookTitle[index], xtitle);
cout << "The title is now " << bookTitle[index];
cout <<"\n\nEnter the first letter of the category you would like to
edit or 'x' to exit: ";
cin >> change;
}
if (change == 'A' || change =='a')
{
cin.ignore();
cout << "Enter the new Author: ";
cin.getline(xauthor,51);
strcpy(author[index], xauthor);
cout << "The author is now " << author[index];
cout <<"\n\nEnter the first letter of the category you would like to
edit or 'x' to exit: ";
cin >> change;
}
if (change == 'P' || change =='p')
{
cin.ignore();
cout << "Enter the new Publisher: ";
cin.getline(xpublisher,31);
strcpy(publisher[index], xpublisher);
cout << "The publisher is now " << publisher[index];
cout <<"\n\nEnter the first letter of the category you would like to
edit or 'x' to exit: ";
cin >> change;
}
if (change == 'D' || change =='d')
{
cin.ignore();
cout << "Enter the new Date: ";
cin.getline(xdate,11);
strcpy(dateAdded[index], xdate);
cout << "The date is now " << dateAdded[index];
cout <<"\n\nEnter the first letter of the category you would like to
edit or 'x' to exit: ";
cin >> change;
}
if (change == 'Q' || change =='q')
{
cout << "Enter the new Quantity on Hand: ";
cin >> xqty;
qtyOnHand[index] = xqty;
cout << "The Quantity on Hand is now " << qtyOnHand[index];
cout <<"\n\nEnter the first letter of the category you would like to
edit or 'x' to exit: ";
cin >> change;
}
if (change == 'W' || change =='w')
{
cout << "Enter the new Whole Sale: ";
cin >> xwholeSale;
wholesale[index] = xwholeSale;
cout << "The Whole Sale is now " << wholesale[index];
cout <<"\n\nEnter the first letter of the category you would like to
edit or 'x' to exit: ";
cin >> change;
}
if (change == 'R' || change =='r')
{
cout << "Enter the new Retail: ";
cin >> xretail;
retail[index] = xretail;
cout << "The retail is now " << retail[index];
cout <<"\n\nEnter the first letter of the category you would like to
edit or 'x' to exit: ";
cin >> change;
}

}
return 0;
}
 
M

Mike Wahler

GRoll21 said:
The problem starts during my while loop. It checks to see if they hit x
or X. If they did, then that means the user wants to exit. Then should
go on and hit the return statement. It just quits. When I hit x, it
pauses for a second then just goes to the next line on the cmd prompt.
My other returns work just fine though. I'm sure its just a stupid
logical error somewhere that I havn't caught. If anyone can point out
any tips that or whats wrong with it that would be great. Thanks again
to all!
while (change != 'x' || change!='X')

while (change != 'x' && change != 'X')


-Mike
 
G

GRoll21

aww man. i really need to take a lil break. something so stuipd i
should of caught. thanks very much though for pointing it out!

- mike
 
M

Mike Wahler

GRoll21 said:
aww man. i really need to take a lil break. something so stuipd i
should of caught. thanks very much though for pointing it out!

It's been my experience that such 'dumb' mistakes
are often the result of trying to write too much
code at once.

Write your code in small increments, and TEST, TEST, TEST!!

(I was able to quickly diagnose the trouble by deleting
99% of that code you posted, replacing it with:

#include <iostream>
using namespace std;

int main()
{
char change(0);
bool condition(false);
cout << "Enter: ";
cin >> change;

while (condition = (change != 'x' || change!='X'))
{
cout << "inside loop, condition == " << condition << '\n';
}

cout << "loop done, condition == " << condition << '\n';
return 0;
}



The problem became immediately obvious.

-Mike
 
J

Jim Langston

GRoll21 said:
The problem starts during my while loop. It checks to see if they hit x
or X. If they did, then that means the user wants to exit. Then should
go on and hit the return statement. It just quits. When I hit x, it
pauses for a second then just goes to the next line on the cmd prompt.
My other returns work just fine though. I'm sure its just a stupid
logical error somewhere that I havn't caught. If anyone can point out
any tips that or whats wrong with it that would be great. Thanks again
to all!

int editBook(int a)
{

///////used to hold new values///
char xisbn[14];
char xtitle[51];
char xauthor[51];
char xpublisher[31];
char xdate[11];
int xqty;
float xwholeSale;
float xretail;
/////////////////////////////////

char change ='z';
char lookUp[51];
bool found = false;

system("cls");
cout << "\n\t\tEnter the title of the book you wish to edit ";
cin >> lookUp;
system("cls");

for(int index = 0; index < 20 && !found; index++)
{
// toupper
char up1[51], up2[51];
for(int position=0; position < 51; position++)
{
up1[position] = toupper(lookUp[position]);
up2[position] = toupper(bookTitle[index][position]);
}
if (strcmp(up1, up2) == 0)
{
found = true;
bookinfo(isbn[index], bookTitle[index], author[index],
publisher[index], dateAdded[index], qtyOnHand[index], wholesale[index],
retail[index]);
}
}

if(!found)
{
cout << "Book not in Inventory";
return 0;
}


cout <<"\n\nEnter the first letter of the category you would like to
edit or 'x' to exit: ";
cin >> change;

while (change != 'x' || change!='X')

This will always be true. You want to AND them, not OR them.

while (change != 'x' && change != 'X' )

same with the rest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top