Parameters

Well, I'll be blunt with you guys. This is an assignment. It was given a week ago. I did everything according to the instructions given, then suddenly my professor said the whole thing is wrong. I mean, it's giving the right output, but he says I didn't do it the way he wanted me to. He said we should use parameters. Thing is, I don't know how. Our previous professor didn't teach us about parameters, so i have no idea how to do it. I hope someone might help. Or please change at least 1 function into a parameter type. I have a defense about my program 2 days from now. Btw, the reason it's so long is because we weren't allowed to use built-in functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#define p printf
#define s scanf

struct STRING{
	char mystring[100];
	char CH;
	char CH2;
	char text[225];
	int index;
	int i;
	int length;
	int choice;	
};

typedef struct STRING str;			
typedef str *string;				  

void LENGTH(str &st);				
void CHARAT(str &st);				
void REPLACE(str &st);				
void INITCAP(str &st);				
void UPPER(str &st);
void LOWER(string &ptr);			
void REVERSE(string &ptr);			
void SUBSTRING(string &ptr);
void INSTRING(string &ptr);
void OCCURRENCE(string &ptr);
void SPLIT(string &ptr);
void INSERT(string &ptr);

main()
{
	str st;							//structure variable
	string ptr;						//pointer variable
	ptr=(string)malloc(sizeof(str));
	
//main menu
	do
	{
		system("cls");
		
		p("*Choose your Pokemon!*");
		p("\n\nFUNCTIONS:");
		p("\n\n\t\t [1] LENGTH()");
		p("\n\t\t [2] CHARAT()");
		p("\n\t\t [3] REPLACE()");
		p("\n\n\t\t SELECT A FUNCTION: ");
		fflush(stdin);
		s("%i",&st.choice);
		
		switch(st.choice)
		{
			case 1:
				LENGTH(st);break;
			case 2:
				CHARAT(st);break;
			case 3:
				REPLACE(st);break;
		}
		p("\n\nPress Y if you want to choose another function.\nPress N if you want to exit. [Y/N]: ");
		fflush(stdin);
		s("%c",&st.CH);
	}
	while(st.CH=='y'||st.CH=='Y');
}
void LENGTH(str &st)
{
	system("cls");
	p("\n Enter String: ");
	fflush(stdin);
	gets(st.mystring);
	
	st.length=0;
	st.index=0;
	while(st.mystring[st.index]!='\0')		
	{
		st.length++;
		st.index++;
	}	
	
	p("\n String length is: %i",st.length);
}
void CHARAT(str &st)
{
	system("cls");
	p("\n Enter String: ");
	fflush(stdin);
	gets(st.mystring);
	
	p("\n Index of the character to grab: ");
	fflush(stdin);
	s("%i",&st.index);
	
	p("\n The character is: %c",st.mystring[st.index-1]);		
}
void REPLACE(str &st)
{
    system("cls");
	p("\n Enter String: ");
	fflush(stdin);
	gets(st.mystring);
	
	st.index=0;
	
	p("\n Character you want to change: ");
	fflush(stdin);
	s("%c",&st.CH);											
	p("\n Character you want to replace: ");
	fflush(stdin);
	s("%c",&st.CH2);										
	p("\n\nOutput: ");
	
	while(st.mystring[st.index]!='\0')
	{
		if(st.mystring[st.index]==st.CH)
		{
			p("%c",st.CH2);
		}
		else
		{
			p("%c",st.mystring[st.index]);	
		}
		st.index++;
	}
}
Um, you're already using parameters. Your function LENGTH, for example, takes a parameter str &st.
Really? Well, my prof turned it into this. And the "hello" is just an example. He wants it to be user input. I don't know how to make the rest of the functions like he did on LENGTH. The first code on my post is what I did. This second one is when he changed it into something I don't know how to do to the rest or make it into user input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#define p printf
#define s scanf

struct STRING{
	char mystring[100];
	char CH;
	char CH2;
	char text[225];
	int index;
	int i;
	int length;
	int choice;	
};

typedef struct STRING str;			
typedef str *string;				  

int LENGTH(char input[]);				
void CHARAT(str &st);				
void REPLACE(str &st);				


main()
{
	str st;							//structure variable
	string ptr;						//pointer variable
	ptr=(string)malloc(sizeof(str));
	
//main menu
	do
	{
		system("cls");
		
		p("*Choose your Pokemon!*");
		p("\n\nFUNCTIONS:");
		p("\n\n\t\t [1] LENGTH()");
		p("\n\t\t [2] CHARAT()");
		p("\n\t\t [3] REPLACE()");
		p("\n\n\t\t SELECT A FUNCTION: ");
		fflush(stdin);
		s("%i",&st.choice);
		
		switch(st.choice)
		{
			case 1:
				case 1:
				printf("String length is %i",LENGTH("hello")); break;
			case 2:
				CHARAT(st);break;
			case 3:
				REPLACE(st);break;
		}
		p("\n\nPress Y if you want to choose another function.\nPress N if you want to exit. [Y/N]: ");
		fflush(stdin);
		s("%c",&st.CH);
	}
	while(st.CH=='y'||st.CH=='Y');
}
void LENGTH(str &st)
{
	system("cls");
	p("\n Enter String: ");
	fflush(stdin);
	gets(st.mystring);
	
	st.length=0;
	st.index=0;
	while(st.mystring[st.index]!='\0')		
	{
		st.length++;
		st.index++;
	}	
	
	p("\n String length is: %i",st.length);
}
void CHARAT(str &st)
{
	system("cls");
	p("\n Enter String: ");
	fflush(stdin);
	gets(st.mystring);
	
	p("\n Index of the character to grab: ");
	fflush(stdin);
	s("%i",&st.index);
	
	p("\n The character is: %c",st.mystring[st.index-1]);		
}
void REPLACE(str &st)
{
    system("cls");
	p("\n Enter String: ");
	fflush(stdin);
	gets(st.mystring);
	
	st.index=0;
	
	p("\n Character you want to change: ");
	fflush(stdin);
	s("%c",&st.CH);											
	p("\n Character you want to replace: ");
	fflush(stdin);
	s("%c",&st.CH2);										
	p("\n\nOutput: ");
	
	while(st.mystring[st.index]!='\0')
	{
		if(st.mystring[st.index]==st.CH)
		{
			p("%c",st.CH2);
		}
		else
		{
			p("%c",st.mystring[st.index]);	
		}
		st.index++;
	}
}
> He said we should use parameters. Thing is, I don't know how.

The issue is that the parameters you are using are bogus; they are not used for either passing information to the function or retrieving information from it.

For instance, your LENGTH function could very well have been written as:

1
2
3
4
5
6
7
8
9
10
//void LENGTH(str &st)
void LENGTH()
{
        str st ;
        printf("\n Enter String: ");
	gets(st.mystring);

        // ...

}


Instead, get the string from the user in main() and pass the initialized string to the function.
And so on, for the other functions.


Avoid fflush(stdin);. See: http://stackoverflow.com/questions/2979209/using-fflushstdin


And, consider not doing this:
1
2
#define p printf
#define s scanf 


If you want to save on keystrokes while typing, you could use the search/replace facility in the text editor.
I see. I kinda got it, but he said that he didn't want us to use void functions. He wanted us to use parameters. And I really have no idea how to do it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// input: initialized STRING st; initialize it in main()
// result: length of string
int LENGTH( str &st ) 
{
	st.length=0;
	st.index=0;
	while(st.mystring[st.index]!='\0')		
	{
		st.length++;
		st.index++;
	}	
	
        return st.length ; // print out the returned value in main()       
}
THANK YOU SO MUCH. :D
Topic archived. No new replies allowed.