GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Dec-2004, 09:53
ToddSAFM's Avatar
ToddSAFM ToddSAFM is offline
Awaiting Email Confirmation
 
Join Date: Dec 2004
Location: North Carolina
Posts: 75
ToddSAFM is on a distinguished road

Compiling Errors


Ok, so I'm relatively new to c++ and programming altogether. I just started learning c++ a semester ago. What I'm trying to do now is create a program that will allow me to input a word/sentence/whatever and the program will take that sentence and change some (and eventually all) the characters to a predefined value (ie: change every occurance of the letter 'a' to 'e' ) In my code, I've only set one letter to change for testing purposes. The problem is that I'm getting error messages that I don't understand...

Here's the code that I'm trying to compile...

CPP / C++ / C Code:
// Created by Todd Whitford (Dec 2004)

#include <iostream.h>

void getInput ( int& i , char ch , char *a[] );
void translateInput ( int i , char *a[] );
void displayResult ( int i , char a[] );


int main()
{
   char choice = ' ';
   int i = 0;
   char ch;
   char a[50];

   cout << "This program will translate everything you type into NAME HERE" << endl ;
   cout << "which is a cipher that I developed for the encoding of secret messages." << endl ;
   cout << "Presently, translation is only one directional, but full duplex translation " << endl ;
   cout << "can and should be expected soon!" << endl << endl ;
   cout << "Once You have finished inputting all that you wish to input,"<< endl ;
   cout << "PLEASE HOLD DOWN CTRL AND PRESS Z as this will allow you to continue with the " << endl;
   cout << "translation process... Press Y to proceed and N to stop" << endl;
   cin >> choice;

   if ( choice == 'Y' || choice == 'y' )
   {
		getInput ( i , ch , a[i] );
		translateInput ( i , a );
		displayResult ( i , a );
   }
   
   cout << endl << "Thank you for using my tranlator!";

  return 0;
}

void getInput ( int &i , char ch , char *a[] )
{
	cout <<"Enter the sentence: ";
   while ( (ch = cin.get()) != EOF )
   {
    	a[i] = ch;  	
      	i++;      
   }

}


void translateInput ( int i, char *a[] )
{
	for ( int j = 0 ; j < i-1 ; j++ )
	{
		switch (char a[i])
		{
			case 'A':
				a[i] = 'E';
				break;
			case 'a':
				a[i] = 'e';
				break;
			default:
				break;
		}
	}
}

void displayResult ( int i , char a[] )
{
	for( int j = 0 ; j < i-1 ; j++ )
	{
      cout << a[j];
	}
	cout << endl;
}

The Error Messages I'm getting Can Be Viewed Here:


I've tried everything that I can think of, and to no avail. By the way, I'm using a Digital Mars free c++ compiler that I got from my school. How might I fix this, anyone have a clue?

I appreciate any help,

Todd Whitford
ssprodnc.tripod.com
Attached Images
File Type: gif cpperror.gif (10.8 KB, 14 views)
  #2  
Old 14-Dec-2004, 10:08
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by ToddSAFM
Ok, so I'm relatively new to c++ and programming altogether. I just started learning c++ a semester ago. What I'm trying to do now is create a program that will allow me to input a word/sentence/whatever and the program will take that sentence and change some (and eventually all) the characters to a predefined value (ie: change every occurance of the letter 'a' to 'e' ) In my code, I've only set one letter to change for testing purposes. The problem is that I'm getting error messages that I don't understand...


Three things:

In functions getInput() and translateInput, you treat the argument "a" as pointer to char.

So the functions should be something like:

CPP / C++ / C Code:
void getInput ( int& i , char ch , char a[] );
void translateInput ( int i , char a[] );


The switch statement should look something like

CPP / C++ / C Code:
    switch (a[i])

With these changes, I think it should compile with no errors (I can't check it since I don't have digital mars). You still may have to do some work to get its functionality to be what you want, but once it's running, you can do the debug thing.

Regards,

Dave
  #3  
Old 14-Dec-2004, 10:31
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by ToddSAFM
By the way, I'm using a Digital Mars free c++ compiler that I got from my school.
You might also suggest to your instructor that Borland 5.5 is also a free compiler -- and much more up-to-date and full featured I'm told. Or if you need a GUI, Bloodshed's Dev-C++.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #4  
Old 14-Dec-2004, 15:24
ToddSAFM's Avatar
ToddSAFM ToddSAFM is offline
Awaiting Email Confirmation
 
Join Date: Dec 2004
Location: North Carolina
Posts: 75
ToddSAFM is on a distinguished road
Ok, thanks, I appreciate the help, I'll let you know if it works...
  #5  
Old 15-Dec-2004, 19:22
ToddSAFM's Avatar
ToddSAFM ToddSAFM is offline
Awaiting Email Confirmation
 
Join Date: Dec 2004
Location: North Carolina
Posts: 75
ToddSAFM is on a distinguished road
I think what we have here is a mild case of "I don't know what in the world I'm doin'" You see, all that I've really learned so far in the class that I'm in is extremely basic stuff like declaring variables, constants, arrays, various loops, very basic text file manipulation, etc... All straight forward and stuff like that. I've just been bored as that stuff has been so easy for me to pick up, I wanted to do something constructive rather than just finding the average number from some fake company and displaying it in a text file. So, that's what led me to wanting to do this translator....

Now that that is out of the way....
I tried the recommended changes (save for a different compiler, but that's next) and searched the net tirelessly for some help with the errors in compiling that still haunt me...

Once again.. Here's my code:

CPP / C++ / C Code:
// Created by Todd Whitford (Dec 2004)

#include <iostream.h>

void getInput ( int& i , char ch , char *a[] );
void translateInput ( int i , char *a[] );
void displayResult ( int i , char a[] );


int main()
{
char choice = ' ';
int i = 0;
char ch;
char a[50];

cout << "This program will translate everything you type into NAME HERE" << endl ;
cout << "which is a cipher that I developed for the encoding of secret messages." << endl ;
cout << "Presently, translation is only one directional, but full duplex translation " << endl ;
cout << "can and should be expected soon!" << endl << endl ;
cout << "Once You have finished inputting all that you wish to input,"<< endl ;
cout << "PLEASE HOLD DOWN CTRL AND PRESS Z as this will allow you to continue with the " << endl;
cout << "translation process... Press Y to proceed and N to stop" << endl;
cin >> choice;

if ( choice == 'Y' || choice == 'y' )
{
getInput ( i , ch , a[i] );
translateInput ( i , a );
displayResult ( i , a );
}

cout << endl << "Thank you for using my tranlator!";

return 0;
}

void getInput ( int &i , char ch , char a[] )
{
cout <<"Enter the sentence: ";
while ( (ch = cin.get()) != EOF )
{
a[i] = ch; 
i++; 
}

}


void translateInput ( int i, char a[] )
{
for ( int j = 0 ; j < i-1 ; j++ )
{
switch (a[i])
{
case 'A':
a[i] = 'E';
break;
case 'a':
a[i] = 'e';
break;
default:
break;
}
}
}

void displayResult ( int i , char a[] )
{
for( int j = 0 ; j < i-1 ; j++ )
{
cout << a[j];
}
cout << endl;
}


I dunno what this thing is trying to say to me, and my instructor won't return any of my emails.... mabye she's just busy...
Anyway, here's my errors:
ssprodnc.1800-webhosting.com

I'd appreciate any help that can be given, as I need it. And I will try a different compiler immediately to see if that changes anything...

Thanks,
Todd
  #6  
Old 15-Dec-2004, 23:09
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by ToddSAFM
I think what we have here is a mild case of "I don't know what in the world I'm doin'" You see, all that I've really learned so far in the class that I'm in is extremely basic stuff like declaring variables, constants, arrays, various loops, very basic text file manipulation, etc... All straight forward and stuff like that. I've just been bored as that stuff has been so easy for me to pick up, I wanted to do something constructive rather than just finding the average number from some fake company and displaying it in a text file.
Been there, done that. I feel your pain.

Quote:
Originally Posted by ToddSAFM
Once again.. Here's my code:
What happened to your formatting? It's now unreadable. Next time try the preview button before submit to see if everything is copacetic.

Quote:
Originally Posted by ToddSAFM
I dunno what this thing is trying to say to me, and my instructor won't return any of my emails.... mabye she's just busy...
Anyway, here's my errors:
http://ssprodnc.1800-webhosting.com/cpperror02.gif
You'd be better off posting your errors instead of creating a link to an image. That way
1) we don't have to go elsewhere for info
2) we can respond to each error individually
3) It's easier to dissect the error when we can edit it here.

The program is barfing on your third parameter in your getInput() call. You prototype your function as
CPP / C++ / C Code:
void getInput ( int& i , char ch , char *a[] );
The third parameter looks like a pointer to an array (pointer to pointer basically). Then in your main you call it with
CPP / C++ / C Code:
getInput ( i , ch , a[i] );
What is a[j]*? A pointer to char? A char array? Or a single char? Answer that and you'll be well on your way to solving both errors.

*i has been changed to protect the formatting
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #7  
Old 16-Dec-2004, 03:55
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
There is another problem in translateInput; you use j to iterate through the vectors components, but then use i for the switch statement and for the changes done to the vector.

Dave missed it too

Quote:
Originally Posted by davekw7x
The switch statement should look something like

CPP / C++ / C Code:
switch (a[i])

Actually, it should be:

CPP / C++ / C Code:
void translateInput ( int i, char * a )
{
	for ( int j = 0 ; j < i-1 ; j++ )
	{
		switch (a[j])
		{
		case 'A':
			a[j] = 'E';
			break;
		case 'a':
			a[j] = 'e';
			break;
		default:
			break;
		}
	}
}

The char thing was also an error, so Dave was right about it.


Quote:
Originally Posted by WaltP
The program is barfing on your third parameter in your getInput() call. You prototype your function as
CPP / C++ / C Code:
void getInput ( int& i , char ch , char *a[] );
The third parameter looks like a pointer to an array (pointer to pointer basically). Then in your main you call it with
CPP / C++ / C Code:
getInput ( i , ch , a[i] );
What is a[j]*? A pointer to char? A char array? Or a single char? Answer that and you'll be well on your way to solving both errors.

Indeed, you probably wanted to pass the vector by address, but instead you created a vector of pointers to char.... Now, since Walt didn't want to give you all the answers, I won't either, unless you really can't figure it out on yourself.
Anyway, you should know that if you pass a function the name of a vector, it will pass the address of the first element of the vector (it's the same as &a[0]). Also, the vector's elements are stored one after another in memory.
Enough already! The good news is that I got your program running - now you try it!

Best regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #8  
Old 16-Dec-2004, 07:07
ToddSAFM's Avatar
ToddSAFM ToddSAFM is offline
Awaiting Email Confirmation
 
Join Date: Dec 2004
Location: North Carolina
Posts: 75
ToddSAFM is on a distinguished road
Ok, firstly...

Quote:
What happened to your formatting? It's now unreadable. Next time try the preview button before submit to see if everything is copacetic.
Sorry about that... I screwed up somewhere... I'm usually VERY conscious about my code being pretty :-)

Quote:
You'd be better off posting your errors instead of creating a link to an image.
K, I see...

Quote:
What is a[j]*? A pointer to char? A char array? Or a single char? Answer that and you'll be well on your way to solving both errors.
Ok, let me try to explain a little better on how the a[] is gettin' passed around...

So I first started by declaring a[] as a 50 char array. That's what it is an array of characters (or at least what its supposed to be). What it should do from here is get passed to the getInput function...
CPP / C++ / C Code:
getInput ( i , ch , a[i] );
In this function, the array a should be filled with however much info. the user inputs. The variable i is used here to determine how many characters within that array are actually used.
CPP / C++ / C Code:
void getInput ( int &i , char ch , char a[] )
{
   cout <<"Enter the sentence: ";
   while ( (ch = cin.get()) != EOF )
   {
    	a[i] = ch;  	
      	i++;      
   }

}
Once the getInput function has been fulfilled, the array values should be passed on back to the main function
CPP / C++ / C Code:

   if ( choice == 'Y' || choice == 'y' )
   {
		getInput ( i , ch , a[i] );
		translateInput ( i , a );
		displayResult ( i , a );
   }
At the main function, the array should then be passed along with i , my glorious counter, to the translateInput function.

In the translateInput function, I'm using the variable j as a counter comparing its value to the i counter so it'll stop trying to translate the values of my array a[]. By the way, thanks LuciWiz for pointing out that I had the i in the switch, not the j. It would've taken a while to figure that out, once I got the program working...
CPP / C++ / C Code:
void translateInput ( int i, char a[] )
{
	for ( int j = 0 ; j < i-1 ; j++ )
	{
		switch (a[j])
		{
			case 'A':
				a[j] = 'E';
				break;
			case 'a':
				a[j] = 'e';
				break;
			default:
				break;
		}
	}
}

So from here, I would like for the array a[] which has now been filled according to the variable i with fully translated characters, to be sent back to the main function for yet one more action.

Here, it should be sent, along with my good buddy i to the displayResult function...
CPP / C++ / C Code:
 {
		getInput ( i , ch , a[i] );
		translateInput ( i , a );
		displayResult ( i , a );
   }

In this function, I'm using a j variable again to act as a counter against i to display each of the values (characters) in my array a[] . From here, the program should just go back to the main function for some finalization...
CPP / C++ / C Code:
void displayResult ( int i , char a[] )
{
	for( int j = 0 ; j < i-1 ; j++ )
	{
      cout << a[j];
	}
	cout << endl;
}

That is what this program is supposed to do. Not necessarily what the code says it'll do. So as far as:
Quote:
What is a[j]*? A pointer to char? A char array? Or a single char? Answer that and you'll be well on your way to solving both errors.
When I use a[i] or a[j] in the program, it should be looking for one character in the whole array. I believe that the way the prog. is setup, when I use a[i] or a[j], that it is within a loop so that it will eventually use all of the characters within the entire array. But throughout the program's whole, I am trying to pass around the whole array, so that the whole array can be inputted into, translated, and displayed.

I guess my real question is probably regarding passing around arrays. I never learned that in class... I think the instructor may have written that down on the board the day of the final exam, but I was too busy trying to leave that place to get home

Anyway, I'll put up all the code once more, so that it'll be formatted correctly. Sorry to bother everyone with these problems as I'm sure there are more pressing needs on the forum than my own...

CPP / C++ / C Code:
// Created by Todd Whitford (Dec 2004)

#include <iostream.h>

void getInput ( int& i , char ch , char *a[] );
void translateInput ( int i , char *a[] );
void displayResult ( int i , char a[] );


int main()
{
   char choice = ' ';
   int i = 0;
   char ch;
   char a[50];

   cout << "This program will translate everything you type into NAME HERE" << endl ;
   cout << "which is a cipher that I developed for the encoding of secret messages." << endl ;
   cout << "Presently, translation is only one directional, but full duplex translation " << endl ;
   cout << "can and should be expected soon!" << endl << endl ;
   cout << "Once You have finished inputting all that you wish to input,"<< endl ;
   cout << "PLEASE HOLD DOWN CTRL AND PRESS Z as this will allow you to continue with the " << endl;
   cout << "translation process... Press Y to proceed and N to stop" << endl;
   cin >> choice;

   if ( choice == 'Y' || choice == 'y' )
   {
		getInput ( i , ch , a[i] );
		translateInput ( i , a );
		displayResult ( i , a );
   }
   
   cout << endl << "Thank you for using my tranlator!";

  return 0;
}

void getInput ( int &i , char ch , char a[] )
{
	cout <<"Enter the sentence: ";
   while ( (ch = cin.get()) != EOF )
   {
    	a[i] = ch;  	
      	i++;      
   }

}


void translateInput ( int i, char a[] )
{
	for ( int j = 0 ; j < i-1 ; j++ )
	{
		switch (a[j])
		{
			case 'A':
				a[j] = 'E';
				break;
			case 'a':
				a[j] = 'e';
				break;
			default:
				break;
		}
	}
}

void displayResult ( int i , char a[] )
{
	for( int j = 0 ; j < i-1 ; j++ )
	{
      cout << a[j];
	}
	cout << endl;
}

OH yeah, I'll put the errors in manually as well :-)

Code:
getInput ( i , ch , a[i] ); Error: need explicit cast for function parameter 3 to get from: char to: char ** translateInput ( i , a ); Error: need explicit cast for function parameter 2 to get from: char * to: char **
(is the [code] tag alright for error messages? I'm not sure about that, thats just the only one I know other than for c++)

Thanks for all the help... everyone.. in past replies, and future ones as well!!

Later,
Todd SAFM
Irregularly Regular Person
  #9  
Old 16-Dec-2004, 07:52
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Yes, the tag is OK, so is the formatting.
Anyway, I guess by your post you say you had no luck in following our directions?
As promissed, I will give you my version of the program. You probably had something in mind, but I can't figure out right now how to change your program to better suit your original idea. Here is my code (yours, actually, with minor modifications):

CPP / C++ / C Code:
#include "stdafx.h"
#include <iostream>
#include "conio.h"

using namespace std;



void getInput ( int& i , char ch , char * a );
void translateInput ( int i , char * a );
void displayResult ( int i , char * a );


int _tmain(int argc, _TCHAR* argv[])
{
	char choice = ' ';
	int i = 0;
	char ch = ' ';
	char a[50];

	cout << "This program will translate everything you type into NAME HERE" << endl ;
	cout << "which is a cipher that I developed for the encoding of secret messages." << endl ;
	cout << "Presently, translation is only one directional, but full duplex translation " << endl ;
	cout << "can and should be expected soon!" << endl << endl ;
	cout << "Once You have finished inputting all that you wish to input,"<< endl ;
	cout << "PLEASE HOLD DOWN CTRL AND PRESS Z as this will allow you to continue with the " << endl;
	cout << "translation process... Press Y to proceed and N to stop" << endl;
	cin >> choice;

	if ( choice == 'Y' || choice == 'y' )
	{
		getInput ( i , ch , &a[0] );
		translateInput ( i , a );
		displayResult ( i , a );
	}

	cout << endl << "Thank you for using my translator!";

	getch();
	return 0;
}

void getInput ( int &i , char ch , char *a )
{
	cout <<"Enter the sentence: ";
	while ( (ch = cin.get()) != EOF )
	{
		a[i] = ch;    
		i++;      
	}
}


void translateInput ( int i, char * a )
{
	for ( int j = 0 ; j < i-1 ; j++ )
	{
		switch (a[j])
		{
		case 'A':
			a[j] = 'E';
			break;
		case 'a':
			a[j] = 'e';
			break;
		default:
			break;
		}
	}
}

void displayResult ( int i , char * a )
{
	for( int j = 0 ; j < i-1 ; j++ )
	{
		cout << a[j];
	}
	cout << endl;
}

My code is VC .NET compliant, so you'll have to probably delete the stdafx.h include, and change the name of the main method to ... main

The rest should be allright for your compiler too.
In case you have trouble following my code, or want a different solution, please post back and we'll do our best to help you.

Best regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #10  
Old 16-Dec-2004, 08:03
ToddSAFM's Avatar
ToddSAFM ToddSAFM is offline
Awaiting Email Confirmation
 
Join Date: Dec 2004
Location: North Carolina
Posts: 75
ToddSAFM is on a distinguished road
I appreciate the help, and I'll try out your version as soon as I get the time. I think I'm still going to try to work out the flaws with my code first, as I don't want to just give up on it... That would'nt work to well with Chivalry (I've been kinda on this big thing lately about following the old code). Anyway, your help is respected, and I'll try some different compilers... Mabye the problem's just with this old Digital Mars piece.

Thanks...

Todd SAFM
Irregularly Regular Person
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How can we trap errors in Constructors (C++) Poolan C++ Forum 6 19-Nov-2004 08:41
help to debug complier errors nkhambal C Programming Language 3 04-Oct-2004 09:26
Wierd errors (again) crystalattice C++ Forum 3 15-Aug-2004 21:02
kernel compiling & Geforce updating crystalattice Computer Software Forum - Linux 5 16-Jun-2004 08:38
Can somebody look at this and point out any errors to me soulfly C Programming Language 7 31-Mar-2004 10:45

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:05.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.