Logical Error. Not Returning

Discussion in 'C++' started by GRoll21, Oct 31, 2005.

  1. GRoll21

    GRoll21 Guest

    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;
    }
     
    GRoll21, Oct 31, 2005
    #1
    1. Advertising

  2. GRoll21

    Mike Wahler Guest

    "GRoll21" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Oct 31, 2005
    #2
    1. Advertising

  3. GRoll21

    GRoll21 Guest

    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
     
    GRoll21, Oct 31, 2005
    #3
  4. GRoll21

    Mike Wahler Guest

    "GRoll21" <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Oct 31, 2005
    #4
  5. GRoll21

    Jim Langston Guest

    "GRoll21" <> wrote in message
    news:...
    > 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.
     
    Jim Langston, Nov 1, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. muser
    Replies:
    4
    Views:
    917
    Jim Fischer
    Jun 26, 2003
  2. sachin bond
    Replies:
    0
    Views:
    270
    sachin bond
    Sep 11, 2003
  3. Karl Heinz Buchegger
    Replies:
    8
    Views:
    317
    Alexander Terekhov
    Sep 12, 2003
  4. DannyB
    Replies:
    13
    Views:
    653
    Paul McGuire
    Feb 22, 2006
  5. Eric

    Logical Error

    Eric, Aug 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    364
    Morten Wennevik
    Aug 9, 2006
Loading...

Share This Page