Thread: Problems with Looping

  1. #16
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    = is for assignment, == for comparison

  2. #17
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    I wrote the program and it works perfectly, but there's a catch.

    Code:
    #include <iostream.h>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string date;
       char answer; //either yes or no
       bool check;
    
      do{cout << "\nplease enter the date in dd/mm/yyyy: "; cin >> date; cout << "\nIs this the correct date ? press y/n \n" << date << "\n\n";  cin >> answer; if (answer == 'y'){ cout << "\nThank you!!\n"; break; } else if (answer != 'n' && answer != 'y') { cout << "invalid input\n"; } } while (answer == 'n' && answer != 'n' || answer != 'y'); system ("pause"); return 0;
    }

  3. #18
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    i got it to loop and put it in my program, (right at the end. last 7 lines)...

    but, i have a problem. When a non integer is inputted, the program crashes.
    how do i get around this ?

    Code:
    #include <iostream.h>
    #include <string>
    #include <conio.h>
    main()
    {
    int salescode, bikeprice, modelcode, quantity;
       string date;
       char answer; //either yes or no
    
    
       do
            //the instructions to enter the i.d. code will be repeated (do...while
            //loop) as long as the salescode is less than 1 or greater than 20.
       	{
    
          	cout << "\n\nPlease enter your identifiction code ";
             cin >> salescode;
    
             //the condition is tested
             if (salescode < 1 || salescode > 20)
    
             	{
                	cout << ("\nTHIS I.D. CODE IS NOT VALID. TRY AGAIN \n");
                }
          }
    	//the instructions to enter the i.d. code will be repeated (do...while
       //loop) as long as the salescode is less than 1 or greater than 20.
       while (1 > salescode || salescode > 20);
    
       //another do...while loop
       do
       	{
          	cout << "\nPlease enter the bike price (one unit = 1 Euro) ";
          	cin >> bikeprice;
    
             if (bikeprice < 1 || bikeprice > 500)
    
             	{
                	cout << ("\nINVALID PRICE. Bike prices cannot be 0 or more than 500. TRY AGAIN \n");
                }
          }
    
       while (bikeprice < 1 || bikeprice > 500);
    
       //another do...while loop
       do
       	{
          	cout << "\nPlease enter the 3 digit model code ";
          	cin >> modelcode;
    
             if (modelcode < 0 || modelcode > 999)
    
             	{
                	cout << ("\nINVALID MODEL CODE. TRY AGAIN \n");
                }
          }
    
       while (modelcode < 0 || modelcode > 999);
    
       //another do...while loop
       do
       	{
             cout << ("\nPlease enter the quantity sold ");
          	cin >> quantity;
    
             if (quantity < 1 || quantity > 10)
    
             {
             	cout << ("\nINVALID QUANTITY. TRY A NUMBER FROM 1-10 \n");
             }
          }
    
       while (quantity < 1 || quantity > 10);
    
    	do {
    
    
    			cout << "\nPlease enter the date in this format dd/mm/yyyy: ";
             cin >> date;
    
    			cout << "\nIs this the correct date ?\t" << date << "\tpress y/n: \n\n";
             cin >> answer;   //gets answer from user
    
    			switch (answer)	{
             	case 'y':
                	cout << "\n\n\nThank You!\n\n";
                   break;
                }
             }while (answer != 'y');
       }

  4. #19
    Registered User
    Join Date
    Apr 2010
    Posts
    38
    Shouldn't those be AND operators? saying OR means that only on side has to be true for it to continue.

  5. #20
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    Quote Originally Posted by fadlan12 View Post
    Shouldn't those be AND operators? saying OR means that only on side has to be true for it to continue.

    nope, does not work.
    a number cannot be smaller than 1 AND bigger than 10

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As to the actual question you asked, have you read the FAQ yet, particularly Cprogramming.com FAQ > Convert a string to a int (C++) ?

  7. #22
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    Quote Originally Posted by tabstop View Post
    As to the actual question you asked, have you read the FAQ yet, particularly Cprogramming.com FAQ > Convert a string to a int (C++) ?
    the thing is, my compiler does not recognize the language.
    Unfortunately, we HAVE to use borland c++ 5.02 !
    Any idea how to stop the program from crashing when a non character is inputted ? - in regards to my program preceding this reply ?
    thanks

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Which part doesn't it recognize?

    You can't use >> by definition since that will not read in a character.

  9. #24
    Registered User
    Join Date
    Apr 2010
    Posts
    25
    Quote Originally Posted by tabstop View Post
    Which part doesn't it recognize?

    You can't use >> by definition since that will not read in a character.
    Info :Compiling E:\program\noname00.cpp
    Error: noname00.cpp(1,2):Unable to open include file 'SSTREAM.h' ??????????????
    Warn : STRING.h(549,3):Functions containing for are not expanded inline
    Warn : STRING.h(557,3):Functions containing while are not expanded inline
    Warn : STRING.h(563,3):Functions containing for are not expanded inline
    Warn : STRING.h(575,3):Functions containing for are not expanded inline
    Warn : string.cc(686,32):Comparing signed and unsigned values
    Error: noname00.cpp(37,25):Undefined symbol 'istringstream' ??????????
    Error: noname00.cpp(37,25):Statement missing ; ?????????????
    Error: noname00.cpp(39,17):Undefined symbol 'myStream' ???????
    Warn : noname00.cpp(43,2):Parameter 's' is never used
    Warn : noname00.cpp(43,2):Parameter 'i' is never used

  10. #25
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    are you trying to accomplish something like this

    Code:
    #include <string>
    #include <iostream>
    #include "input.h" //function prototype of double readDouble(string prompt, bool allowNegatives)
    using namespace std;
    
    int main()
    {
       double month;//use whatever u want here
       double day;
       double year;
       char resp;
       char no='n';
        yes='y';
       do{
          double month=readDouble("Enter a month ",false);
          double day=readDouble("Enter a day ",false);
          double cin>>year=readDouble("Enter a year ",false);
          cout<<"Is this date correct "<<month<<"/"<<day<<"/"<<year<<endl;
          cin>>resp;
       }while(resp=='n'||resp==no);
       //while(resp=='y'||resp==yes)
       //put whatever u want here
    }
    
    double readDouble(string prompt, bool allowNegatives)
    {
    	double rv=0.0;
    	bool done=false;
    	do
    	{
    	cout<<prompt;
    	cin>>rv;
    	while(cin.fail()!=0)
    	{
    		cerr<<"Cannot read data!"<<endl;
    		cin.clear();
    		cin.ignore(256,'\n');
    		cout<<prompt;
    		cin>>rv;
    		}
    	if (allowNegatives==false && rv<0)
    	{
    		cerr<<"Input positive numbers!"<<endl;
    		cin.clear();
    		cin.ignore(256,'\n');
    		cin>>rv;
    		}
    	else
    	{
    	done=true;
    	}
    
    	return rv;
    	}
    	while(done==false);
    }
    breakup code however needed,
    now that im looking at this the char no='n' and char yes='y' are not needed
    Last edited by Creatlv3; 05-04-2010 at 01:37 AM. Reason: took a closer look at my program, fixed mistakes made by tiredness

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with nesting in looping
    By wobbles in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2010, 02:30 AM
  2. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  3. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 06:38 PM
  4. tile colliision detection problems
    By werdy666 in forum Game Programming
    Replies: 1
    Last Post: 10-23-2002, 09:26 PM
  5. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM