I have an assignment where I need to input a certain value, and the computer will then run through an 'txt' file to see if the value is in there, otherwise it will ask you if you want to create a new value or make a new input.

ifstream infil("kundregister.txt");//declares outstream 'utfil' to 'kundregister.txt'
  cout<<"...och customernumber: ";
  cin>>soktkund;//input of custombernumber typed by the user
  while (infil.getline(namn,29))//while loopen checks that the customernumber exists in 'kundregister.txt'
  {
   //Reads name and address with getline
   infil.getline(adress,39);
   infil >> kundnr;
    infil.get();
  if (soktkund==kundnr)// condition that the input value is the same as the customernumber from 'kundregister.txt'
  {
   hittkund=1;// the custombernumber exists in 'kundregister.txt' the program continues
   break;
  }// condition closes
  }// the loop closes
  if(!hittkund)// if the input customernumber doesn't exist in 'kundregister.txt' the following text will show on the screen:
  cout<<"kundnummer saknas";

So after this I'm supposed to give the user 2 options, either 'Enter an other customernumber' or 'Return to the menu'

I have tried for ages, but can't find a solution :(

Any help is most appreciated.

Regards,
Kim

Please post your code in tags.

You can prompt the user to enter either 1 or 2. Then use while loop to decide what to do with the input.

int choice;
while(choice==1)
{
//enter customer number
}

The while loop will execute until the user enters a number other than 1.
I was also thinking about using an if/else statement.

if(choice==1)
{
//enter customer number
}
else if(choice==2)
{
//return to menu
}
else
{
//print illegal input message
}

So what should I do about the:

cin>>soktkund;//input of custombernumber typed by the user
  while (infil.getline(namn,29))//while loopen checks that the customernumber exists in 'kundregister.txt'

I want to enter these 2 options when the user inputs the wrong customernumber. But it just continues on to the next loop. I want it to either restart the loop or just quit the loop and return to the menu.

BTW, thanks for the quick replies :) Sorry about the code tag thing. I should of had read the rules, but I have been working with this thing for so long so I'm just so eager to finish it :)

Regards,
Kim

In its current state, you intend the while loop to check if the cutomer number is in the file. So, if the customer number is NOT in the file, the loop wont execute. I think your if statement should come after you close the while loop. Is that clear? Let me reiterate. If the while loop fails, it wont execute the if statement becuase it is contained within the while loop. You want the if statement to execute if the while fails. So this is why I think you should move the if statement out of the while loop. I hope that was clear.

So I took 'if' out of the while-loop. But I can't get it too work either way
. I'm sorry but I'm really in programming.

ifstream infil("kundregister.txt");////declares outstream 'utfil' to 'kundregister.txt'
while (infil.getline(namn,29//while loop checks that the customer number exists in 'kundregister.txt'
  {
   //Reads name and address with getline
   infil.getline(adress,39);
   infil >> kundnr;
    infil.get();
	cout<<"Your customer number: ";
  cin>>soktkund;////input of custombernumber typed by the user
  }//loop closes
  if (soktkund==kundnr// condition that the input value is the same as the customernumber from 'kundregister.txt'
  {
   hittkund=1// the custombernumber exists in 'kundregister.txt' the program continues
   break;
  }
  else
  {
	cout << "Customber number is missing"; //says that the customer number is missing

  }

So I removed (!hittkund) Should I have done that?

Regards,
Kim

>So I removed (!hittkund) Should I have done that?
Why would you remove this? I thought that was what you were trying to test. If I understand correctly, I think you want to output an error message if the customer number is not in the file. So this if condition would be needed correct?

I thought t would be harder doing those 2 options if I made it that way so instead I used

else
  {
	cout << "Customer number is missing";

  }

It has the same effect hasn't it, but thought it would be easier?
The problem is that I don't know what to do next.
Thanks

Regards,
Kim

Try to get the input before the while loop, make the while loop checks if the input is in kundregister.txt and only execute If statement if condition isn't met. The algorithm should be something like this:

get input
while (input is !condition)
execute If statement
end while

since you don't seem to want to execute the If statement if the input is in the txt file.

I'm not sure what language your using, but not(soktkund==kundnr) is not the same as not(hittkund).
You are using different variables.
By the way, the if statement on line 11 is missing a ')'

alternatively, you can

get input
if (input is in txt file) 
goto end
else execute while

i don't have a compiler on my comp yet lol so can't test the code for you but the key is to get the algorithm right first, and then you can code correctly

:)

I had the input before the while loop before didn't I If you look at my first post.

ifstream infil("kundregister.txt");//declares outstream 'utfil' to 'kundregister.txt'
  cout<<"Type your customer number: ";
  cin>>soktkund;//input of custombernumber typed by the user
  while (infil.getline(namn,29))//while loopen checks that the customernumber exists in 'kundregister.txt'
  {
   //Reads name and address with getline
   infil.getline(adress,39);
   infil >> kundnr;
    infil.get();
  if (soktkund==kundnr)// condition that the input value is the same as the customernumber from 'kundregister.txt'
  {
   hittkund=1;// the custombernumber exists in 'kundregister.txt' the program continues
   break;
  }// condition closes
  }// the loop closes
  if(!hittkund)// if the input customernumber doesn't exist in 'kundregister.txt' the following text will show on the screen:
  cout<<"kundnummer saknas";

So how do I get the while loop to not execute the if statement if it's wrong?
How do I tell it to instead output two questions that either restarts the loop or either return you to the menu, if I input the wrong customer number?

Regards,
Kim

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.