Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Program keeps looping [Solved]

Program keeps looping [Solved]

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CounterClockWise
    wrote on last edited by
    #1

    I'm new to C++ programming and also new to posting on forums. I'm sorry if I don't enter this correctly for my first post. I am trying to create a program that will display a predetermined shipping charge for 2 zip codes. Program should send error messages if the zip code entered is not 5 digits long, not all 5 numbers, or does not begin with 605 or 606. Below is the code I have written so far. Sentinel value of -1 should end the program. The messages seem to be correct when I enter test data but it keeps looping the same message repeatedly and I have to close the program to stop it. Can anyone tell me what I've done wrong? #include #include #include using namespace std; //function prototype char verifyNumbers(string); int main () { //declare variables string zip = " "; char isAllNumbers = ' '; const string invalid_length = "Invalid length"; const string not_all_numbers = "Not all numbers"; const string invalid_shipping = "Zip code is not valid for shipping charges"; //get input (zip code) cout << "Enter 5 digit zip code (-1 to end): "; getline(cin, zip); //verify input while (zip != "-1") { if (zip.length() == 5) { isAllNumbers = verifyNumbers(zip); if (isAllNumbers == 'Y') { if (zip.find ("605", 0) == 0 || zip.find ("606", 0) == 0) { if (zip.find("605", 0) == 0) { cout << "Shipping is $25" << endl; } else cout << "Shipping is $30" << endl; //end if } else cout << invalid_shipping << endl; //end if } else cout << not_all_numbers << endl << endl; //end if } else cout << invalid_length << endl << endl; //end if cout << "Enter 5 digit zip code (-1 to end): "; getline(cin, zip); } //end while //calculate shipping charges //display output } //end of main function //*****function definitions***** char verifyNumbers(string zip) //determine if each character is a number { //declare variables string currentChar = ""; int x = 0; char isNumber = 'Y'; //determine if characters are all numbers while (x < 5 && isNumber == 'Y') { currentChar = zip.substr(x, 1); if (currentChar >= "0" && currentChar <= "9") x += 1; else isNumber = 'N'; //end if }//end while return isNumber; } //end of verifyNubmers function I really appreciate any help I can get! Thank you! Carla

    Richard Andrew x64R 1 Reply Last reply
    0
    • C CounterClockWise

      I'm new to C++ programming and also new to posting on forums. I'm sorry if I don't enter this correctly for my first post. I am trying to create a program that will display a predetermined shipping charge for 2 zip codes. Program should send error messages if the zip code entered is not 5 digits long, not all 5 numbers, or does not begin with 605 or 606. Below is the code I have written so far. Sentinel value of -1 should end the program. The messages seem to be correct when I enter test data but it keeps looping the same message repeatedly and I have to close the program to stop it. Can anyone tell me what I've done wrong? #include #include #include using namespace std; //function prototype char verifyNumbers(string); int main () { //declare variables string zip = " "; char isAllNumbers = ' '; const string invalid_length = "Invalid length"; const string not_all_numbers = "Not all numbers"; const string invalid_shipping = "Zip code is not valid for shipping charges"; //get input (zip code) cout << "Enter 5 digit zip code (-1 to end): "; getline(cin, zip); //verify input while (zip != "-1") { if (zip.length() == 5) { isAllNumbers = verifyNumbers(zip); if (isAllNumbers == 'Y') { if (zip.find ("605", 0) == 0 || zip.find ("606", 0) == 0) { if (zip.find("605", 0) == 0) { cout << "Shipping is $25" << endl; } else cout << "Shipping is $30" << endl; //end if } else cout << invalid_shipping << endl; //end if } else cout << not_all_numbers << endl << endl; //end if } else cout << invalid_length << endl << endl; //end if cout << "Enter 5 digit zip code (-1 to end): "; getline(cin, zip); } //end while //calculate shipping charges //display output } //end of main function //*****function definitions***** char verifyNumbers(string zip) //determine if each character is a number { //declare variables string currentChar = ""; int x = 0; char isNumber = 'Y'; //determine if characters are all numbers while (x < 5 && isNumber == 'Y') { currentChar = zip.substr(x, 1); if (currentChar >= "0" && currentChar <= "9") x += 1; else isNumber = 'N'; //end if }//end while return isNumber; } //end of verifyNubmers function I really appreciate any help I can get! Thank you! Carla

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      The thing that's causing the infinite looping is that once you enter the "verify input" loop, there is no way out of it so that the user can enter another number. Put the prompt and the call to getline inside the while (zip != "-1") loop so that if the user enters an incorrect value, they can enter another value on the next iteration of the loop.

      The difficult we do right away... ...the impossible takes slightly longer.

      C 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        The thing that's causing the infinite looping is that once you enter the "verify input" loop, there is no way out of it so that the user can enter another number. Put the prompt and the call to getline inside the while (zip != "-1") loop so that if the user enters an incorrect value, they can enter another value on the next iteration of the loop.

        The difficult we do right away... ...the impossible takes slightly longer.

        C Offline
        C Offline
        CounterClockWise
        wrote on last edited by
        #3

        That worked perfectly. Thank you so much! Thanks, Carla

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups