Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 12th, 2006, 4:59 PM   #1
a3orange
Newbie
 
Join Date: Apr 2006
Posts: 14
Rep Power: 0 a3orange is on a distinguished road
Need Help With A Program

Hi, I'm testing a math problem for school,and I have a problem with the program I wrote to test it. Here is the [math] problem:

Joe has a deck of 100 cards. Each one has one yellow side, and one red side and each one is numbered from 1 to 100. All cards start with yellow facing up. He then turns around any cards that have a number divisible by 2. He does this for all numbers up to 100. How many cards have their yellow faces up at the end of this process?

Here is the code:
#include <iostream>
#include <string>
using namespace std;
const int decksize = 100;
int main()
{
	string cards[decksize] = "Yellow";
	int divby;
	for (divby == 2; divby < decksize; divby++)
	{
        int i;
		for (i = 0; i < decksize; i++)
		{
			if (cards[i] == "Yellow" && (i + 100) % divby == 0)
			{
				cards[i] = "Red";
			}
			else if (cards[i] == "Red" && (i + 100) % divby == 0)
			{
				cards[i] == "Yellow";
			}
		}
	}
int yellow;
int red;	
int n;
	for (n = 0; n < decksize; n++)
	{
		if (cards[n] == "Red")
		red++;
		else if (cards[n] == "Yellow")
		yellow++;
	}
cout << "Yellow: " << yellow << endl;
cout << "Red: " << red << endl;
cin.get();
cin.get();
return 0;
}
The program always crashes at this line:
if (cards[i] == "Yellow" && (i + 100) % divby == 0)
a3orange is offline   Reply With Quote
Old Apr 12th, 2006, 5:15 PM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
your definition of cards is inaccurate. You define it as a 100 element array of strings, and then you initialize it to one string.
What's the error you're getting?
OpenLoop is offline   Reply With Quote
Old Apr 12th, 2006, 5:18 PM   #3
Bench
Newbie
 
Join Date: Feb 2006
Posts: 20
Rep Power: 0 Bench is on a distinguished road
	int divby;
	for (divby == 2; divby < decksize; divby++)
look carefully at that line..
Bench is offline   Reply With Quote
Old Apr 12th, 2006, 8:02 PM   #4
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
How many cards have their yellow faces up at the end of this process?
50. No need to write the program. :p
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Apr 12th, 2006, 8:47 PM   #5
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
man InfoGeek, my company sure can use a man like you. Can you compute the resulting balance of 3,500,000 policies after 6,000,000+ transactions a day? Let me know so we can return the mainframes back to IBM
OpenLoop is offline   Reply With Quote
Old Apr 12th, 2006, 8:53 PM   #6
a3orange
Newbie
 
Join Date: Apr 2006
Posts: 14
Rep Power: 0 a3orange is on a distinguished road
OpenLoop:

Doesn't that make all elements of the array strings?

Thank you, bench, but the program is really messed up now, it returns the results:

Yellow: 121
Red: -2056523657

Infogeek:

I'm eventually going to make the program so the user can enter in the amount of cards and how high 'divby' goes. Also, I'm going to make it so there can be more then 2 colord on each of the cards. Maybe I'll make them pyramids.
a3orange is offline   Reply With Quote
Old Apr 12th, 2006, 9:14 PM   #7
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
string cards[10] <== this defines ten consecutive strings
char cards[10] <== this here is ten consecutive characters that can be called a string
string card <== this is one string

now, in your case, you're comparing cards[i] for a string. My guess is, you want an array where each element is a string. That would be the first case above.

EDIT: maybe that's what you need:
string cards[] = {"Yellow","Red","Green","Black"};
OpenLoop is offline   Reply With Quote
Old Apr 12th, 2006, 9:26 PM   #8
a3orange
Newbie
 
Join Date: Apr 2006
Posts: 14
Rep Power: 0 a3orange is on a distinguished road
OpenLoop:

Didn't I already do that with:

string cards[decksize] = "Yellow";

Maybe I need to add curly braces around "Yellow"?
a3orange is offline   Reply With Quote
Old Apr 12th, 2006, 9:31 PM   #9
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
int yellow;
int red;	
int n;
	for (n = 0; n < decksize; n++)
	{
		if (cards[n] == "Red")
		red++;
		else if (cards[n] == "Yellow")
		yellow++;
	}
You need to give the yellow and red variables an initial value. At the moment they are just getting whatever value is in memory at the time.
The Dark is offline   Reply With Quote
Old Apr 12th, 2006, 9:34 PM   #10
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by a3orange
OpenLoop:

Didn't I already do that with:

string cards[decksize] = "Yellow";

Maybe I need to add curly braces around "Yellow"?
It is still only one value of "Yellow" that you are trying to assign into an array of values.
Try something like:
string cards[decksize];
for (int i = 0; i < decksize; i++)
  cards[i] = "Yellow";
The Dark 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




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

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