Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 3rd, 2008, 9:23 PM   #1
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 256
Rep Power: 2 Jabo is on a distinguished road
do while loop error or not

I have this little program (book excercise) that prints a pattern until you enter anything but Y or y. It works fine until I don't enter y or Y, then I get a runtime error stating "Run-Time Check Failure #2 - Stack around the variable 'times' was corrupted." and when I break it, it's on line 37 which is my final closing brace. During my troubleshooting, I also noticed it won't break if times is less than 10 (single digit entries). I tried using cin.get() and cin.getline() and it will still break if the number is more than 9. I notice there are still variables in the locals window for ans, times, input, and again when it reaches line 37, but I guess they aren't cleaned up until after line 37.

in the call stack, I have this "03_10_24.exe!main() Line 37 + 0xf bytes" highlighted

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. char input;
  7. char times[2];
  8. char again;
  9. int ans;
  10. do
  11. {
  12. cout<<"What character to use for the pattern?\n";
  13. input=cin.get();
  14. cin.ignore(1);
  15. cout<<"Your pattern will be "<<input<<endl;
  16. cout<<"How many lines(from 1 to 20)?\n";
  17. cin.getline(times,3);
  18. ans=atoi(times);
  19. cout<<"You will print "<<ans<<" lines with this pattern.\n";
  20. for (int x=0;x<ans;x++)
  21. {
  22. for (int y=0;y<=x;y++)
  23. cout<<input;
  24. cout<<endl;
  25. }
  26. for (int x=ans;x>0;x--)
  27. {
  28. for (int y=x;y>0;y--)
  29. cout<<input;
  30. cout<<endl;
  31. }
  32. cout<<"Run new pattern? (y or Y)\n";
  33. again=cin.get();
  34. cin.ignore(1);
  35. }while (again=='y'||again=='Y');
  36. return 0;
  37. }

I can't figure out what the problem is, anybody see anything wrong? I don't mean with the logic of the program, but with the syntax; I'm still working the logic out. I'm thinking it has to be the way I'm handling the input into the char array, any suggestions?

Last edited by Jabo; Jul 3rd, 2008 at 9:43 PM.
Jabo is offline   Reply With Quote
Old Jul 3rd, 2008, 9:35 PM   #2
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 256
Rep Power: 2 Jabo is on a distinguished road
Re: do while loop error or not

Big nevermind, it was with my array size, I was pulling in 3 chars into a 2 char array, duh.
Jabo is offline   Reply With Quote
Old Jul 4th, 2008, 9:00 AM   #3
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 641
Rep Power: 4 Jessehk is on a distinguished road
Re: do while loop error or not

I know this isn't related to your problem, but have you considered using std::string's and C++ methods of converting strings?

I usually do something like this when I encounter this sort of thing in C++:
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <sstream>
  3. #include <stdexcept>
  4. #include <string>
  5.  
  6. class InvalidConversion : public std::invalid_argument {
  7. public:
  8. explicit InvalidConversion( const std::string &msg ) :
  9. std::invalid_argument( msg ) {}
  10. };
  11.  
  12. template <typename T>
  13. T fromString( const std::string &str ) {
  14. std::stringstream ss( str );
  15. T result;
  16.  
  17. if ( ! (ss >> result) )
  18. throw InvalidConversion( "Can't convert" );
  19.  
  20. return result;
  21. }
  22.  
  23. int main() {
  24. int x = fromString<int>( "132" );
  25. float y = fromString<float>( "3.14159" );
  26.  
  27. std::cout << x << std::endl;
  28. std::cout << y << std::endl;
  29. }
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Jul 4th, 2008, 6:26 PM   #4
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 256
Rep Power: 2 Jabo is on a distinguished road
Re: do while loop error or not

I have used the String class before, but it's been 2 years since I've done anything with C++ (in school) and I'm just going back through my book re-learning everything. At this point, the book hasn't covered strings.
Jabo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
server-infinite loop programmingnoob C 19 Oct 1st, 2006 10:52 PM
Value of index incorrect after loop aznluvsmc C 13 Nov 6th, 2005 9:47 PM
ASCII conversion loop difficulty crmpicco Visual Basic 2 May 3rd, 2005 1:55 PM
WinSock accept() hangs program in Do loop... layer C++ 5 Apr 29th, 2005 11:28 AM
Timing loop problems badbasser98 C++ 11 Mar 10th, 2005 8:30 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC