Variable seems to crash program after is has been passed.

I cannot figure out how to fix my line 33 error. I believe I have successfully passed the variable "rows" but it then crashes!? Is it because it is defined incorrectly in the prototype?

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  //Libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

//Function Prototypes
char ReadInRecipientsName(void);
int  OutputToFileORstdOut(void);
//int TreeToFile(void);
int  AskUserForRows(void);
int CreateGreetingCard(int inVal, char a[75]);


//Begin
int main ()
{
	//Local Declarations
	char myString;
	int choice;
	int inVal = 0;
	int choice2;
	
	

	//Function Calls
	myString = ReadInRecipientsName();

	choice = OutputToFileORstdOut();

	//choice2 = TreeToFile();

	AskUserForRows();

	inVal = CreateGreetingCard(inVal, myString);

	//Displaying Stuff

	//Exit Program
	return 0;
}
//******************************************FUNCTIONS**************************************
//Function ReadInRecipientsName
	char ReadInRecipientsName(void)
	{
		//Local Declarations
		char a[75];

		//User Prompt
		printf("Please enter recipient: ");
		scanf("%s", &a);
		strlen("a"); 
	
		//Return
		return a[75];
	}
//Function OutputToFileORstdOut
	int OutputToFileORstdOut(void)
	{
		//Local Declarations
		int b = 0;

		//User Prompt
		printf("Please enter a number greater than zero to output to the stdOut \n or enter a number less than zero to output to a file: ");
		scanf("%d", &b);

		//Return
		return b;
	}
//Function AskUserForRows
	int AskUserForRows(void)
	{
		//Local Declarations
		int localVal = 0;

		//User Prompt
		printf("Please enter the number of rows: ");
		scanf("%d", &localVal);
	
		//Return
		return localVal;
	}
//Function TreeToFile
	int TreeToFile(void)
	{
		//Local Declarations
		FILE* fp1;
		char FileName1;
		//User Prompt
		printf("Please enter a name for your file. \n");
		scanf("%s", &FileName1);
		fp1 = fopen ("Libraries\Documents\FileName1.txt", "w");
		printf("Your Christmas card was placed here at -> C:\\\\%s");
	}
//Function CreateGreetingCard
	int CreateGreetingCard(int inVal, char a[75])
	{
		//Local Declarations
		int i;
		int k;
		int rows;
		int location;
		char myString[75];

		//Work
		//Top of border
		for (i=1;i<=80;++i)
		{
		printf("_");
		}
		printf("\n");

		//Chrsitmas Tree Build
		for (k=1;k<=(rows-1);++k)
		{
			printf("|");
			for (i=1;i<=79;++i)
			{
				if(((39-(k-1))<=i) && (i <= (39+(k-1))))
				{
					printf("*");
				}
				else
				{
						printf(" ");
				}
			}
			printf("|\n");
		}
		for (i=1;i<=81;++i) //tree base
		{
			if ((i==1) || (i==80))
			{
				printf("|");
			}
			if ((i>37)&&(i<41))
			{
				printf("*");
			}
			else
			{
				printf(" ");
			}
		}
		printf("\n");
		if(rows<60)//empty space
		{
		for (k=(rows+1);k<=60;++k)
			{
				for (i=1;i<=81;++i)
					{
					if ((i==1) || (i==81))
					{
						printf("|");
					}
					else
					{
						printf(" ");
					}
					}
				printf("\n");
			}
		}
		
		location = floor((float)(((19+strlen(myString))/2)));
		for(i=1;i<=81;i++) //greeting message centered at bottom of card
		{
			if(i==1 || i==81)
			{
				printf("|");
			}

			if (i==(39-location))
			{
				printf("Happy Holidays:%s!!",myString);
				i=i+(21+strlen(myString));
			}

			else
			{
				printf(" ");
			}
		}
			printf("\n");
			for(i=1;i<=80;++i)
			{
				printf("-");
			}
	}
AskUserForRows() returns an int, which you never assign to anything. You also have a local variable called inVal in your main which you've initialized to zero, and so you're passing zero to CreateGreetingCard().


inVal = AskUserForRows();

inVal = CreateGreetingCard(inVal, myString);


Edit: You're not passing your string correctly to CreateGreetingCard(). You cannot return an array from a function. You can return a pointer to your array, and then pass it into your function.

Alternatively, you can use C++ strings, cin, cout, and avoid a lot of this mess... unless you have to write this using C headers only.
Last edited on
In CreateGreetingCard rows is undefined. Also inVal is not used
Topic archived. No new replies allowed.