Thread: Need a little help with my project

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    16

    Need a little help with my project

    First of all, I'd like to introduce myself to the C-Programming.com boards. I'm taking CS in college and not going to class for the summer made me rusty. If anyone can provide me with some insight with why I get errors when building this program, it would be helpful.


    Code:
    #include <cstring>
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    void encode(char[] ,char[] ,char[] );
    
    void main()
    {
    	char table[5][10];
    	char statement[50];
    	char keyword[10];
    
    	cout<<"Please enter your statement,max 50 characters, all lowercase, with no spaces."<<endl;
    	cin.get(statement,50);
    	cout<<"Please enter your keyword. Do not repeat any letters.";
    	cin>>keyword;
    	
    	cout<<"Our original statement is "<<statement<<endl;
    	
    	encode(statement,keyword,table);
    
    	for (int y=0;y<strlen(keyword);y++)
    		{				
    		for (int x=0;x<ceil(strlen(statement)/strlen(keyword))+1;x++)
    			{
    			cout<<table[x][y];
    			}
    		cout<<' ';		
    		}										
    		
    	cout<<endl<<endl;
    	
    }
    
    void encode(char statement[], char keyword[], char table[])
    {
    	int i=0;
    	int keylength = strlen(keyword);
    	int messagelength = strlen(statement);
    
    	for(int x=0;x<ceil(messagelength/keylength)+1;x++)		  
    		for(int y=0;y<keylength;y++)	
    		{
    			table[x][y] = statement[i];
    			i++;
    		}
    }
    Thanks in advance. ^_^

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    >>#include <cstring>

    wrong header... <string> for str functions (strlen)

    for C-style strings it's <ctype>


    >>encode(statement,keyword,table);

    you need to speciy one of the dimensions of table...

    encode (statement,keyword,table[x][]);
    Last edited by major_small; 09-08-2003 at 07:43 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    It would help if you give the errors your getting.

    Just a few things I noticed:

    1. int main( ), not void main( )
    2. encode takes three char* args, but you pass it two char*s and a char** (table is a char**)

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by major_small
    >>#include <cstring>
    wrong header... <string> for str functions (strlen)
    <cstring> is right... <string> is the Std C++ Lib 'string' class.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    oh really? I always used <string>... okey dokey then... ignore that one
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Hmm... Kinda strange that it worked.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    16
    All right, I changed my function header to: void encode(char statement[], char keyword[], char table[5][])
    and my function call to: encode(statement,keyword,table[5]);

    however, this gives me another error:
    '<Unknown>' Missing Subscript
    and points to my function header before my function code.

    Once again I am lost, and the way things are going, I'm going to be here alot more often this semester.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    i guess so... I just compiled a program a few times with both headers, and there was no difference at all...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Hmm... right... The problem is you implemented both of our solutions. While table is a char**, table[x] (where x is 0, 1, ..., 4) is a char*. Based on the fact that the encode function actually uses both subscripts, I'd say your probably looking for just changing the function prototype to have a char**, and not using table[x] as a char*.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    16
    I apologize for my stupidity, but...wha?

    My new semester's resolution is not to take the summer off of using C++.

    Anyway, you lost me there.

    Code:
    #include <cstring>
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    void encode(char[] ,char[] ,char**);
    
    int main()
    {
    	char table[5][10];
    	char statement[50];
    	char keyword[10];
    
    	cout<<"Please enter your statement,max 50 characters, all lowercase, with no spaces."<<endl;
    	cin.get(statement,50);
    	cout<<"Please enter your keyword. Do not repeat any letters.";
    	cin>>keyword;
    	
    	cout<<"Our original statement is "<<statement<<endl;
    	
    	encode(statement,keyword,table[5]);
    
    	for (int y=0;y<strlen(keyword);y++)
    		{				
    		for (int x=0;x<ceil(strlen(statement)/strlen(keyword))+1;x++)
    			{
    			cout<<table[x][y];
    			}
    		cout<<' ';		
    		}										
    		
    	cout<<endl<<endl;
    
    	return 0;
    }
    
    void encode(char statement[], char keyword[], char table[5][])
    {
    	int i=0;
    	int keylength = strlen(keyword);
    	int messagelength = strlen(statement);
    
    	for(int x=0;x<ceil(messagelength/keylength)+1;x++)		  
    		for(int y=0;y<keylength;y++)	
    		{
    			table[x][y] = statement[i];
    			i++;
    		}
    }
    That is exactly what I have right now.

    Once again I apologize for my stupidity. Once I re-familirize myself with the language, I won't have any lapses like that.

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    here's one way to do it (the way I was talking about before):

    Code:
    #include <cstring>
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    void encode(char*,char*,char*);
    
    void main()
    {
    	char table[5][10];
    	char statement[50];
    	char keyword[10];
    
    	cout<<"Please enter your statement,max 50 characters, all lowercase, with no spaces."<<endl;
    	cin.getline(statement,50,'\n');
    	cout<<"Please enter your keyword. Do not repeat any letters.";
    	cin>>keyword;
    	
    	cout<<"Our original statement is "<<statement<<endl;
    	
                    for(int x=0;x<5;x++)
    	          encode(statement,keyword,table[x][]);
    
    	for (int y=0;y<strlen(keyword);y++)
    		{				
    		for (int x=0;x<ceil(strlen(statement)/strlen(keyword))+1;x++)
    			{
    			cout<<table[x][y];
    			}
    		cout<<' ';		
    		}										
    		
    	cout<<endl<<endl;
    	
    }
    ...
    that probably doesn't do what you want it to (I didn't look into encode), but it's just a prototype that you should be able to work off of... I would actually go with what Zach L. said, because I would tend to agree with him on this one...
    Last edited by major_small; 09-08-2003 at 08:22 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    16
    I want to pass the entire 2D array "table" to the function.

    What the function does is take the message, insert it into the table in row-major order. After this, the message is printed in column major order. The actual keyword is arbitrary, because the important part is the length of the keyword, as we do not have to sort the array. (originally, we had to sort the array and print it based on the alphabetical order of the letters in the keyword, but too many people were having trouble implementing this.)

  13. #13
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    #include <cstring>
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    void encode(char[] ,char[] ,char[][10]);
    
    int main()
    {
    	char table[5][10];
    	char statement[50];
    	char keyword[10];
    
    	cout<<"Please enter your statement,max 50 characters, all lowercase, with no spaces."<<endl;
    	cin.get(statement,50);
    	cout<<"Please enter your keyword. Do not repeat any letters.";
    	cin>>keyword;
    	
    	cout<<"Our original statement is "<<statement<<endl;
    	
    	encode(statement,keyword,table);
    
    	for (int y=0;y<strlen(keyword);y++)
    		{				
    		for (int x=0;x<ceil(static_cast<double>(strlen(statement)/strlen(keyword)))+1;x++)
    			{
    			cout<<table[x][y];
    			}
    		cout<<' ';		
    		}										
    		
    	cout<<endl<<endl;
    
    	return 0;
    }
    
    void encode(char statement[], char keyword[], char table[][10])
    {
    	int i=0;
    	int keylength = strlen(keyword);
    	int messagelength = strlen(statement);
    
    	for(int x=0;x<ceil(static_cast<double>(messagelength/keylength))+1;x++)		  
    		for(int y=0;y<keylength;y++)	
    		{
    			table[x][y] = statement[i];
    			i++;
    		}
    }
    I get errors about ceil() being ambiguous, but I've never heard of ceil function before...


    Edit: looked up ceil(), a simple cast fixed my errors...should compile fine now
    Last edited by JaWiB; 09-08-2003 at 08:52 PM.
    "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

  14. #14
    Registered User
    Join Date
    Sep 2003
    Posts
    16
    Wow, that worked...well except for one thing:

    I use the phrase "allyourbasearebelongtous" as my input and "movezig" as my keyword.

    The output is:

    Please enter your statement,max 50 characters, all lowercase, with no spaces.
    allyourbasearebelongtous
    Please enter your keyword. Do not repeat any letters.movezig
    Our original statement is allyourbasearebelongtous
    abbo laeu lsls yeo oan¨d urg¨d ret¨d

    Press any key to continue

    What are those weird symbols after the 5th 6th and 7th sets of letters and how can I rid myself of them?

    Or more simply put, how do I initialize a character array so I don't get anything like that?
    Last edited by Shinobi-wan; 09-08-2003 at 09:13 PM.

  15. #15
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    The weird symbols are because you don't initialize the contents of table and you don't fill it up all the way in encode. Your statement is 24 characters and your key is seven characters. So you have seven sets of letters and you add a letter to each set until you have added all 24 letters, but you leave the last four spots open. When you output the table you are outputting four letters per set, regardless of how many letters have been added to that set.

    You could initialize table in the beginning using memset. If you set it to all 0's then you can check for a 0 in the table and stop outputting when you reach it. Or you could add a counter to stop outputting when the message length is reached. Or you could initialize to the ' ' (space) character and it will output spaces instead of gobbledy-gook.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM