4 Replies - 237 Views - Last Post: 12 September 2012 - 06:43 PM Rate Topic: -----

#1 JMurray308  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-September 12

for some reason my c++ code is giving me the wrong result

Posted 12 September 2012 - 06:08 PM

[

//This program demonstrates a linerar search to verify lottery numbers present or not //present in ten tickets.   


#include <iostream>
using namespace std;

//function prototypes
int searchList(int [], int, int);
const int SIZE = 10;
const int NUM_ROWS = 2;
const int NUM_COL = 5;

int main()
{   

	int results, value;
	int number[SIZE] = {13579, 26791, 26792, 33445, 55555,62483, 77777, 79422, 85647, 93121};
	//int number;


	results = searchList(number, SIZE, 10);
	cout << "Please enter the ticket number:    " <<endl;

	cin >> value;
	if (results == -1)
		cout << "You did not win the lottery.\n";
	else
	{
		cout << "You won the lottery";
		cout << (results + 1) << ".\n";
	}
	return 0;
}




int searchList(int list[], int size, int value)
{
	int index = 0;
	int position = -1;
	bool found = false;

	while (index < size && !found)
	{
		if(list[index] == value)
		{
			found = true;
			position = index;
		}
		index++;
	}
	return position;
}

]

This post has been edited by GunnerInc: 12 September 2012 - 06:10 PM
Reason for edit:: added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: for some reason my c++ code is giving me the wrong result

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 719
  • View blog
  • Posts: 1,976
  • Joined: 28-March 11

Re: for some reason my c++ code is giving me the wrong result

Posted 12 September 2012 - 06:11 PM

Instead of closing, I will do some prodding....

What results do you expect? What results are you getting?
Was This Post Helpful? 0
  • +
  • -

#3 JMurray308  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 08-September 12

Re: for some reason my c++ code is giving me the wrong result

Posted 12 September 2012 - 06:24 PM

View PostGunnerInc, on 12 September 2012 - 06:11 PM, said:

Instead of closing, I will do some prodding....

What results do you expect? What results are you getting?

I entered the right numbers to win the lottery and my results say I did not win the lottery???
I should have won the lottery according to my code. I think my situation is faulting in the entry of the
chance value for a winner. I can not find out how to enter a value other than cin as i have it.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1688
  • View blog
  • Posts: 2,553
  • Joined: 21-June 11

Re: for some reason my c++ code is giving me the wrong result

Posted 12 September 2012 - 06:30 PM

Note that you never use the variable input after storing the user input in it. So clearly the behavior of the program will be the same, no matter what the user enters.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1897
  • View blog
  • Posts: 5,690
  • Joined: 05-May 12

Re: for some reason my c++ code is giving me the wrong result

Posted 12 September 2012 - 06:43 PM

On line 23, you search the list for the number 10, and record the success or failure of that operation in the variable results. Later in lines 27-34, you report the results of the search. Since 10 is not in the list of winning numbers, of course it will report that you lost.

I think that sepp2k meant to say variable value.

This post has been edited by Skydiver: 12 September 2012 - 06:49 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1