12 Replies - 2843 Views - Last Post: 19 September 2011 - 08:20 PM Rate Topic: -----

#1 ikevin09  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 12-September 11

Help with my code

Posted 19 September 2011 - 06:30 PM

Instruction:

"Lottery Winners
A lottery ticket buyer purchases 10 tickets a week, always playing the same 10 5-digit “lucky” combinations. Write a program that initializes an array or a vector with these numbers and then lets the player enter this week’s winning 5-digit number. The program should perform a linear search through the list of the player’s numbers and report whether or not one of the tickets is a winner this week. Here are the numbers:
13579 26791 26792 33445 55555
62483 77777 79422 85647 93121 "

My code:
#include <iostream>

using namespace std;

const int size = 10;
int arrLottery[size]={13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121};

int Search (int [], int, int);

int main()
{
	int target, result;

	cout<<"Please enter this week's 5-digit winning lottery number: ";
	cin>>target;

	result = Search(arrLottery, size, target);
	
	if (result != -1)
	{
		cout<<"you have a winning ticket."<<endl;
	}
	else
	{
		cout<<"You did not win this week."<<endl;
	}
	
	cin.get(); cin.get();
	return 0;
}

int Search (int arrLottery[], int Max, int target)
{
	int index;

	for (index = 0; index < Max-1; index++)
	{
		if (arrLottery[index] == target)
			return 0;
	}
	return -1;
}


Can someone tell me what's wrong here? I tested it out and it works perfectly. Myprogramminglab won't accept it, saying my standard output is not what was expected. But I was actually following their IO format.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with my code

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Help with my code

Posted 19 September 2011 - 07:12 PM

for (index = 0; index < Max-1; index++)


Your not iterating over the entire array. The condition should be index < Max.
Was This Post Helpful? 0
  • +
  • -

#3 ikevin09  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 12-September 11

Re: Help with my code

Posted 19 September 2011 - 07:15 PM

I fixed that, but it still reports "your standard output is not what was expected"
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Help with my code

Posted 19 September 2011 - 07:22 PM

Well, everything looks correct. Perhaps what your writing to the standard output stream isn't what the lab expects to see. Were you provided with examples of output?

you have a winning ticket.
You did not win this week.


Is that what the lab expects to see?
Was This Post Helpful? 0
  • +
  • -

#5 ikevin09  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 12-September 11

Re: Help with my code

Posted 19 September 2011 - 07:30 PM

Yeah, there are 2 screenshots of how the program should looks like. I followed everything included in the screenshots.

Here is the sample I/O:

Please enter this week's 5-digit winning lottery number: 85647
You have a winning ticket.




Please enter this week's 5-digit winning lottery number: 12345
You did not win this week.

Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Help with my code

Posted 19 September 2011 - 07:48 PM

cout<<"you have a winning ticket."<<endl;


The beginning of the sentence isn't capitalized, therefore your output doesn't match what's expected.
Was This Post Helpful? 0
  • +
  • -

#7 ikevin09  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 12-September 11

Re: Help with my code

Posted 19 September 2011 - 07:51 PM

Hm ... still "your standard output is not what was expected" >.<

"Remarks:
     ⇒     Checking output
     ⇒     Your standard output is not what was expected.

Your code standard input
5
Your code standard output
Please enter this week's 5-digit winning lottery number: You did not win this week."

This post has been edited by ikevin09: 19 September 2011 - 07:53 PM

Was This Post Helpful? 0
  • +
  • -

#8 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Help with my code

Posted 19 September 2011 - 08:00 PM

Shouldn't the answer be on a separate line. E.g.

cout<<"\nYou have a winning ticket."<<endl;


Is this lab checker on-line? Somewhere that I can access it myself.
Was This Post Helpful? 0
  • +
  • -

#9 ikevin09  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 12-September 11

Re: Help with my code

Posted 19 September 2011 - 08:04 PM

I don't think it is .. =/

and yea, the output is in separate line.

This post has been edited by ikevin09: 19 September 2011 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#10 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Help with my code

Posted 19 September 2011 - 08:07 PM

If this is the output of your program:

Please enter this week's 5-digit winning lottery number: 5You did not win this week.

That doesn't match what's expected:

Please enter this week's 5-digit winning lottery number: 5
You did not win this week.


Did you try adding the newline?
Was This Post Helpful? 0
  • +
  • -

#11 ikevin09  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 12-September 11

Re: Help with my code

Posted 19 September 2011 - 08:13 PM

http://imageshack.us...40/outputr.png/
I added a new line but it still the same. I wonder if using pointer is valid in this program?
Was This Post Helpful? 0
  • +
  • -

#12 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Help with my code

Posted 19 September 2011 - 08:16 PM

Try removing the prompt, perhaps its looking for just the answer. The checker is probably feeding the input to stdin of your program.

cout<<"Please enter this week's 5-digit winning lottery number: ";


You'll need to remove that new line from the answer string as well.

I really have no other ideas. Good luck.

This post has been edited by blackcompe: 19 September 2011 - 08:19 PM

Was This Post Helpful? 0
  • +
  • -

#13 ikevin09  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 12-September 11

Re: Help with my code

Posted 19 September 2011 - 08:20 PM

lol .. well thank you for your help, I'll try to figure it out somehow.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1