easy fix

This is a discussion on easy fix within the C++ Programming forums, part of the General Programming Boards category; ok, for some reason, it is one of those days where I cannot fix simple problems. Anywhere j is used ...

  1. #1
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79

    easy fix

    ok, for some reason, it is one of those days where I cannot fix simple problems. Anywhere j is used in this code, I either get left operand must be l-value, or I get pointer erros. Take a look.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    
    
    int getrand(int maxsize);
    void printdeck(int deck[], int suit[]);
    void shuffle(int deck[], int suit[]);
    
    int prevrandnum=0;
    
    int main()
    {
    	int deck[13];
    	int suit[4];
    	for (int i=0; i<13; i++) deck[i]=i;
    	for (int j=0; j<4; j++) suit[j]=j;
    	printdeck(suit, deck);
    	shuffle(suit, deck);
    	printdeck(suit, deck);
    	return 0;
    }
    
    void printdeck(int deck[], int suit)
    {
    	for(int i=0; i<13; i++)
    	{
    		cout<<deck[i];
    		if(i<12) cout<<", ";
    		else cout<<". ";
    	}
    	for(int j=0; j<4; j++)
    	{
    		cout<<suit[j];
    		if(j<3) cout<<", ";
    		else cout<<". ";
    	}
    }
    void shuffle(int deck[], int suit)
    {
    	int size=13;
    	int result[13];
    	while(size>0)
    	{
    		int rand=getrand(size);
    		result[13-size]=deck[rand];
    		for(int i=rand+1; i>size; i++) deck[i-1]=deck[i];
    		size=size-1;
    	}
    	for (int i=0; i<14; i++) deck[i]=result[i];
    
    
    
    
    
    
    
    
    	int size1=4;
    	int result1[4];
    	while(size1>0)
    	{
    		int rand=getrand(size1);
    		result1[4-size]=suit[rand];
    		for(int j=rand+1; j>size1; j++) suit[j-1]=suit[j];
    		size1=size1-1;
    	}
    	for (int j=0; j<5; j++) suit[j]=result1[j];
    
    
    
    
    
    
    
    	deck=result;
    	suit=result1;
    }
    int getrand(int maxsize)
    {
    	if(prevrandnum==0)srand(time(NULL));
    	prevrandnum=rand();
    	return (rand()%maxsize);
    }
    This has been a public service announcement from GOD.

    111 1111

  2. #2
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    You forgot the [] ate the defition of the funtions to the suit variable.
    Nothing more to tell about me...
    Happy day =)

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Just one more thing. I do not know what compiler you use, but my compiler pointed the error correctly. Im using Dev 4.9.8.0.
    Nothing more to tell about me...
    Happy day =)

  4. #4
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    thanks, I will try that, I am using visual c++ 6.0 (I know I shouldn't be!)
    This has been a public service announcement from GOD.

    111 1111

  5. #5
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    The newest version of Dev-C++ is a ton better than MSVC++. I just switched from MSVC++ to Dev-C++ not to long ago and I am extremely happy with it. The GUI is almost complete, the online patcher is awesome now, the help file is out, it uses GCC 3.2. It's NOT microsoft!! hehe!

  6. #6
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    hmmm... ok, take the code above, and I am getting these errors now:

    (29) : error C2109: subscript requires array or pointer type
    (33) : error C2374: 'j' : redefinition; multiple initialization
    (27) : see declaration of 'j'
    (65) : error C2109: subscript requires array or pointer type
    (66) : error C2109: subscript requires array or pointer type
    (66) : error C2109: subscript requires array or pointer type
    (66) : error C2106: '=' : left operand must be l-value
    (69) : error C2109: subscript requires array or pointer type
    (69) : error C2106: '=' : left operand must be l-value
    (78) : error C2440: '=' : cannot convert from 'int [4]' to 'int'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    Error executing cl.exe.

    to me, it makes no sense since the exact same code works with i.
    This has been a public service announcement from GOD.

    111 1111

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You need to make the second parameter of each function an array
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Registered User sentienttoaster's Avatar
    Join Date
    Nov 2002
    Posts
    79
    that helped, thanks for the advise
    This has been a public service announcement from GOD.

    111 1111

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    You should use <cstdlib>.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me fix my mess!!!!
    By Starr in forum C++ Programming
    Replies: 35
    Last Post: 02-01-2006, 02:40 PM
  2. Easy question, (should be) easy answer... ;-)
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-12-2002, 09:36 PM
  3. Replies: 20
    Last Post: 05-25-2002, 07:14 PM
  4. EASY GUI development.. like with Qt?
    By rezonax in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2001, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21