8 Replies - 2869 Views - Last Post: 12 November 2007 - 01:34 PM Rate Topic: -----

#1 Binary mick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 30-October 07

logic error in a do while loop

Post icon  Posted 11 November 2007 - 02:11 PM

Hello again just need some help on this.

This program puts names into a array Database_Names but when run the following comes out:


What is your Name: Martin
Do you want to put any more names in true/false: true
What is your Name: Do you want to put any more names
in true/false:What is your Name: Do you want to put any more names in true/false:What is your Name: Do you want to put any more names in true/false:What is your

and so on.


#include <iostream>
#include <string>
using namespace std;

bool more=true;
int a = 1;
bool See_the_base=true;
string Database_Names[101];
int c;
int main()
{


do
{
	 
 cout<<"What is your Name: ";
 cin>>Database_Names[a];
 a++;
 
cout<<"Do you want to put any more names in true/false: ";
cin>>more;
 
}
while (more==true);

cout<<"Do you want to see the database: true/false :";
cin>>See_the_base;

if (See_the_base == true)//print out the database

{
				 a=c;
				 
				 for (int b=1;b<c;b++)
				 {
					 a=-1;
					 a++;
					 cout<<Database_Names[a]<<"/t";
					 
					 }
					 }
					 else{
						  cout<<"end";
						  }
						  }


It just goes passed the cin>>Database_Names[a]; and even a cin.get(); command.I cant find the reason why it loops.

Thanks for any help in finding out were the loop went wrong.

This post has been edited by Binary mick: 11 November 2007 - 02:13 PM


Is This A Good Question/Topic? 0
  • +

Replies To: logic error in a do while loop

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: logic error in a do while loop

Posted 11 November 2007 - 02:19 PM

Hi, change this line:
bool more=true;
with
bool more=false;
Was This Post Helpful? 0
  • +
  • -

#3 Binary mick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 30-October 07

Re: logic error in a do while loop

Posted 11 November 2007 - 02:33 PM

Thanks that solved the inf loop but now it just ends the program when i put true in it.Any ideas.

This post has been edited by Binary mick: 11 November 2007 - 02:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: logic error in a do while loop

Posted 11 November 2007 - 04:22 PM

You haven't initialized the variable c to any value at any point. You set the value of a equal to c and use that as the stop condition in the for-loop, but since it probably has a value of zero (NOT something you can count on, however), the loop doesn't cycle through and display the names. You should probably be just setting the stop condition in the loop to the value of a, without going through an intermediary variable.

The use of the variable a as an index into the array of strings doesn't make much sense in this case, especially since you first set it equal to -1 and then increment it, every time you go through the loop. This means that the same element will be displayed each time. Since you're already using b as the control variable in the display loop, why don't you use it to access the elements of the array? Perhaps something like this:
if (See_the_base == true) {//print out the database
   for (int b=0;b<a;b++) {
	   cout<<Database_Names[b]<<"\t";
   }
}


Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

#5 Binary mick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 30-October 07

Re: logic error in a do while loop

Posted 11 November 2007 - 04:51 PM

o i messed that thanks.but my problem is the do while loop true or false input will make the program now just print out \t.

This post has been edited by Binary mick: 11 November 2007 - 04:54 PM

Was This Post Helpful? 0
  • +
  • -

#6 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: logic error in a do while loop

Posted 11 November 2007 - 05:06 PM

Oops, that escape sequence has the wrong slash - the tab character is '\t', not '/t'.

If you mean there's a problem with something else, you should probably repost your current code, because a bunch of changes have been made here, and we can't always guess what you've done. And be really careful about proper indentation with the posted code, because the first version you posted is very difficult to read. And I'm unfortunately not using an IDE with autoformat at the moment.
Was This Post Helpful? 0
  • +
  • -

#7 Binary mick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 30-October 07

Re: logic error in a do while loop

Posted 11 November 2007 - 05:51 PM

its only the do while loop thats getting me.when i put in true or false it ends the program.

#include <iostream>
#include <string>
using namespace std;

bool more=false;
int a = 1;
bool See_the_base=false;
string Database_Names[101];

int main()
{


do
{
	
cout<<"What is your Name: ";
cin>>Database_Names[a];
a++;

cout<<"Do you want to put any more names in true/false: ";
cin>>more;

}
while (more==true);

cout<<"Do you want to see the database: true/false :";
cin>>See_the_base;

if (See_the_base == true)//print out the database

{
				;
				
				 for (int b=1;b<a;b++)
				 {
					 
					 a++;
					 cout<<Database_Names[a]<<"\t";
					
					 }
					 }
					 else{
						  cout<<"end";
						  }
						  }


Was This Post Helpful? 0
  • +
  • -

#8 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 45
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: logic error in a do while loop

Posted 11 November 2007 - 05:59 PM

The string "true" is not the same as a boolean true, ditto for false. Try entering 1 or 0 instead, since that's what the program is actually expecting here, and it should work. I didn't really look all that closely at that part, and I was just entering 1s and 0s because that's what they mean in my mind. If you want to actually have the user type in "true", you're going to need to get the input in a string, and use a string comparison rather than the equality operator.

And your for loop in the display step is still screwy - skip incrementing the variable a, and output Database_Names[b] instead. The variable a is already set to the maximum index in the array, so don't mess with that in there. The index variable b keeps track of where you are in the array, while the variable a keeps track of how many elements there are in Database_Names.

You should also change the intialization of a to a=0, and start the display loop with b=0, not b=1. Array indices run from 0...max_val-1, not 1...max_val, so you're skipping over the first element (leaving it empty in the input step, and not displaying it during the output step).
Was This Post Helpful? 0
  • +
  • -

#9 Binary mick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 30-October 07

Re: logic error in a do while loop

Posted 12 November 2007 - 01:34 PM

Thanks i would have never got that 1/0 problem i started off in java. The var's were just prove that you should no watch movies up to 3am and then start coding.
Thanks again.

Btw
A representative of M$ came into my class today and gave us a challenge .There is no way in hell i can do these sorts of challenges but you guys might.
http://imaginecup.com/

This post has been edited by Binary mick: 12 November 2007 - 01:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1