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 15-Aug-2004, 12:47
harsha harsha is offline
New Member
 
Join Date: Mar 2004
Location: Malaysia
Posts: 12
harsha is on a distinguished road
Unhappy

Calling functions


I have this 3 function i want to call them in main. I did but i dont know whetever my parameters are correct or not cause it gives me arrays saying i cant convert parameter. I think my logic is wrong but i dont know where PLEASE help to corret it. Thank You.

CPP / C++ / C Code:
#include<iostream>
using namespace std;
void copystring(char, char);
void joinstring(char,char);
void comparestring(char, char);

void copystring(char *h1, char *h2)
{

	for(;*h2!='\0'; h2++,h1++)
	{
		*h1=*h2;
	}
		*h1='\0';
}

void joinstring(char *h1, char *h2)
{

	for(;*h1!='\0'; h1++);
	for(;*h2!='\0'; h2++,h1++)
		{
			*h1=*h2;
		}
		*h1='\0';
}

int comparestring(char *h1, char *h2)
{
	int cmp;
	char flg;

	for(;*h1!='\0'; h1++)
	{
		if(*h1!=*h2)
		{
			flg='n';
			if((*h1-*h2)>0)
				cmp=1;
			else
				cmp=-1;
		}
		
		flg='y';
	}
	if(flg=='y')
		cmp=0;
	return cmp;
	
}

void main()
{
	cout<<"CopyString of harha n swetha"<<endl;
	copystring("harsha","swetha");
	cout<<"Joinstring of harsha n swetha"<<endl;
	joinstring("harsha","swetha");
	cout<<"Comparestring of H n S"<<endl;
	comparestring('H','S');
}
  #2  
Old 15-Aug-2004, 21:54
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
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
If you are getting compile errors, you need to tell us EXACTLY what the messages are (copy them into your post) and add a comment on the lines in the posted code so we can find the lines in question.

In your Copy String function, you are attempting to copy the literal text "swetha" over the literal text "harsha". This is illegal. If you could do this, everywhere you use the literal "harsha" after the call, "swetha" will be used instead. This would be very confusing. To illustrate the problem consider this function:
CPP / C++ / C Code:
void switch (int &a, int &b)
{
    int xx;
    xx = *a;
    *a = *b;
    *b = xx;
    return;
}

Call with this main:
CPP / C++ / C Code:
int main()
{
    switch(3,7);
    a = 2+3;
    printf("%d\n", a);
}
The program would output 9, not 5. switch() changes the value 3 into a 7 and 7 into 3. Cute trick! Believe it or not, I did this once in the 70's. It was funny -- and hard to find. Today though it won't happen, your program will crash.

Same type of problem with Join String.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #3  
Old 16-Aug-2004, 09:54
harsha harsha is offline
New Member
 
Join Date: Mar 2004
Location: Malaysia
Posts: 12
harsha is on a distinguished road
I m sorry i know i cant past string "harsha" n "swetha" like that. I made a mistake. I have correct it. I have do the correction for it by user entering the string. But i still have the error saying "'copystring' : cannot convert parameter 1 from 'char [30]' to 'char' ". I dont understand what error this means. If I have any logic mistake please correct me. Thank you.

CPP / C++ / C Code:
#include<iostream>
using namespace std;
void copystring(char, char);
void joinstring(char,char);
void comparestring(char, char);

void copystring(char *h1, char *h2)
{
	char *h=h1;

	for(;*h2!='\0'; h2++,h1++)
	{
		*h1=*h2;
	}
		*h1='\0';
		cout<<"New string is: "<<h<<endl;

}

void joinstring(char *h1, char *h2)
{
	char *h=h1;

	for(;*h1!='\0'; h1++);
	for(;*h2!='\0'; h2++,h1++)
		{
			*h1=*h2;
		}
		*h1='\0';
		cout<<"New string is: "<<h<<endl;
}

int comparestring(char *h1,  char *h2)
{
	int cmp;
	char flg;

	for(;*h1!='\0'; h1++)
	{
		if(*h1!=*h2)
		{
			flg='n';
			if((*h1-*h2)>0)
				cmp=1;
			else
				cmp=-1;
		}
		
		flg='y';
	}
	if(flg=='y')
		cmp=0;
	return cmp;
	
}

void main()
{
	char name1[30], name2[30];

	cout<<"Enter first string: "<<endl;
	cin>>name1;
	cout<<"Enter second string: "<<endl;
	cin>>name2;
	copystring(name1, name2);

	cout<<"Enter first string: "<<endl;
	cin>>name1;
	cout<<"Enter second string: "<<endl;
	cin>>name2;
	joinstring(name1, name2);

	cout<<"Enter first string: "<<endl;
	cin>>name1;
	cout<<"Enter second string: "<<endl;
	cin>>name2;
	if(comparestring(name1, name2))

		cout<<"Both same\n";

	else

		cout<<"Not same\n";

}
  #4  
Old 16-Aug-2004, 10:46
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi harsha. I got this to compile as is, but there are some problems. Take a look at your function declarations:
CPP / C++ / C Code:
void copystring(char, char);  //Should be: void copystring(char*, char*);
void joinstring(char,char);  //Should be: void joinstring(char*, char*);
void comparestring(char, char);  //Should be: int comparestring(char*,char*);

I am not sure why you even have them. You don't need to declare your functions unless they are called previous to there definition.

One other thing that my compiler didn't like and probably something to get used to. main should not be void, it should always return an int value. Most O/Ses that I have used will take care of this, but a process should *always* return a value when it finishes.
  #5  
Old 16-Aug-2004, 11:04
harsha harsha is offline
New Member
 
Join Date: Mar 2004
Location: Malaysia
Posts: 12
harsha is on a distinguished road
Thx for your reply but when i do as u ask me to:
CPP / C++ / C Code:
void copystring(char, char);  //Should be: void copystring(char*, char*);
void joinstring(char,char);  //Should be: void joinstring(char*, char*);
void comparestring(char, char);  //Should be: int comparestring(char*,char*);

I still get an error saying " conditional expression of type 'void' is illegal"
'int __cdecl comparestring(char *,char *)' : overloaded function differs only by return type from 'void __cdecl comparestring(char *,char *)'
'comparestring' : redefinition; different basic types

Why i need to declare this is for passing parameters cause without these declaration i can't pass two parameters.
  #6  
Old 16-Aug-2004, 11:10
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Harsha. Did you just add the comments? You need to replace those lines so they are:
CPP / C++ / C Code:
void copystring(char*, char*);
void joinstring(char*, char*);
int comparestring(char*,char*);

And I still say that you can just remove these lines all together. They are not necessary as your main function is after all of your other function implementations.

Alot of people like to explicitly define functions like this, but if it is not needed, I just think it makes another area, where you can have a typo...
  #7  
Old 16-Aug-2004, 11:27
harsha harsha is offline
New Member
 
Join Date: Mar 2004
Location: Malaysia
Posts: 12
harsha is on a distinguished road
Thanks a lot it works now. I just forgot to replace the void with int.

You ask to remove the lines right, I cant do that cause when i do so it gives me an error no declaration of these functions. So I need these 3 lines to call the functions.

Can i ask you what is "typo"?
  #8  
Old 16-Aug-2004, 11:50
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by harsha
Thanks a lot it works now. I just forgot to replace the void with int.

You ask to remove the lines right, I cant do that cause when i do so it gives me an error no declaration of these functions. So I need these 3 lines to call the functions.

Can i ask you what is "typo"?

Sorry "typo" is an english'ism for typing a mistake. For instance typing "void" instead of "int"

What compiler are you using? My compiler (gcc/g++) doesn't have a problem if I remove the decleration lines. Beyond that, I don't know of any compiler that would have a problem based on the structure of C/C++ programs and the way that they are compiled. What errors do you get if you comment out or remove those lines?
  #9  
Old 16-Aug-2004, 12:06
harsha harsha is offline
New Member
 
Join Date: Mar 2004
Location: Malaysia
Posts: 12
harsha is on a distinguished road
I get this errors when i remove the lines:
error C2065: 'copystring' : undeclared identifier
: error C2065: 'joinstring' : undeclared identifier
: error C2065: 'comparestring' : undeclared identifier
: error C2373: 'copystring' : redefinition; different type modifiers
error C2373: 'joinstring' : redefinition; different type modifiers
: error C2373: 'comparestring' : redefinition; different type modifiers


I use Micrososft Visual C++ to do my codings.
  #10  
Old 16-Aug-2004, 12:19
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by harsha
I get this errors when i remove the lines:
error C2065: 'copystring' : undeclared identifier
: error C2065: 'joinstring' : undeclared identifier
: error C2065: 'comparestring' : undeclared identifier
: error C2373: 'copystring' : redefinition; different type modifiers
error C2373: 'joinstring' : redefinition; different type modifiers
: error C2373: 'comparestring' : redefinition; different type modifiers


I use Micrososft Visual C++ to do my codings.

hmmm. I just dusted off V.C. 6.0 and compiled this:
CPP / C++ / C Code:
#include<iostream>
using namespace std;

void copystring(char *h1, char *h2)
{
  char *h=h1;

  for(;*h2!='\0'; h2++,h1++)
  {
    *h1=*h2;
  }
    *h1='\0';
    cout<<"New string is: "<<h<<endl;

}

void joinstring(char *h1, char *h2)
{
  char *h=h1;

  for(;*h1!='\0'; h1++);
  for(;*h2!='\0'; h2++,h1++)
    {
      *h1=*h2;
    }
    *h1='\0';
    cout<<"New string is: "<<h<<endl;
}

int comparestring(char *h1,  char *h2)
{
  int cmp;
  char flg;

  for(;*h1!='\0'; h1++)
  {
    if(*h1!=*h2)
    {
      flg='n';
      if((*h1-*h2)>0)
        cmp=1;
      else
        cmp=-1;
    }

    flg='y';
  }
  if(flg=='y')
    cmp=0;
  return cmp;

}

int main()
{
  char name1[30], name2[30];

  cout<<"Enter first string: "<<endl;
  cin>>name1;
  cout<<"Enter second string: "<<endl;
  cin>>name2;
  copystring(name1, name2);

  cout<<"Enter first string: "<<endl;
  cin>>name1;
  cout<<"Enter second string: "<<endl;
  cin>>name2;
  joinstring(name1, name2);

  cout<<"Enter first string: "<<endl;
  cin>>name1;
  cout<<"Enter second string: "<<endl;
  cin>>name2;
  if(comparestring(name1, name2))

    cout<<"Both same\n";

  else

    cout<<"Not same\n";

	return 0;

}

with no errors or warnings. Is that exactly the way that you had it? It is not a big deal as long as it is working, but I *never* pre-declare my functions unless I need to. I have never had a problem with this on any compiler since I started using C. My main function is always the last function though...
 
 

Recent GIDBlogStupid Management Policies 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
conflict between printf and stdarg.h va functions mirizar C Programming Language 3 12-Jul-2004 09:11
Understanding functions tommy69 C Programming Language 15 15-Mar-2004 18:59
Calling functions within a function spudtheimpaler C Programming Language 5 02-Mar-2004 09:02
New to C...need help calling functions rbeierle C Programming Language 3 10-Feb-2004 19:46

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

All times are GMT -6. The time now is 01:24.


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