Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 04-02-09   #1 (permalink)
PC Gamer
 
KoolMan's Avatar
 
intel ati

Join Date: Jul 2008
Location: SoCal
Posts: 727

Rep: 50 KoolMan is acknowledged by some
Unique Rep: 45
Trader Rating: 3
Default Parameter passing Char to sting (C or C++)

hello
hope there are some of you guys who know C or C++

anyway i have to do pass a char as a sting (ithink )

Code:
#include<stdio.h>

void load(char *name, int *z, int *b, int *t)
{
	printf("Name");
	gets(name);
	printf("enter 3 values ");
	scanf("%d%d%d", &(*z), &(*b), &(*t));
}

void print(char name, int x, int y, int z, float avg)
{
	printf("%sn", name);
	printf("The 3 numbers are %d  %d  %dn", x, y, z);
	printf("the avg is %fn", avg);
}

float calc(int a, int b, int c)

{ 
	int sum;
	float avg;
	sum = a + b + c;
	avg = sum / (float) 3 ;
	return avg;
} 

void main()
{ 
	int a,b,c;
	float avg;
	char name;
	load(&name, &a, &b, &c);
	avg = calc(a, b, c);
	print(name, a, b,c, avg);
}

so without the char name the program works fine once i add it it does not work. asks for Name , then the 3 #, and stops get a windows box saying program stooped working.
hope you guys can help me.
thanks
__________________
iPhone Club

Selling my Gaming Rig

Gaming Rigl

System: KoolMan's RIG
CPU
C2D E8400 (E0)
Motherboard
Asus P5Q Deluxe
Memory
4GB
Graphics Card
VisionTek HD4850
Hard Drive
1 TB
Power Supply
Corsair 750W
Case
RocketFish
CPU cooling
Sigmatek S1283 (water comming soon)
OS
Dual Vista x64 & XP x64
Monitor
Acer 20" Widescreen

Last edited by KoolMan : 04-02-09 at 12:52 AM
KoolMan is offline   Reply With Quote
Old 04-02-09   #2 (permalink)
Case Modder
 
Spotswood's Avatar
 
Join Date: Jul 2008
Location: New Hampshire, USA
Posts: 236

Rep: 46 Spotswood is acknowledged by some
Unique Rep: 39
Trader Rating: 0
Default

The code failed to allocate an array of chars to read the name into i.e. pointer versus array.

Code:
#define GUESS_AS_TO_HOW_MANY_CHARS_NEEDED_FOR_NAME_BUFFER  256

void main()
{ 
	int a,b,c;
	float avg;
	char name[GUESS_AS_TO_HOW_MANY_CHARS_NEEDED_FOR_NAME_BUFFER];
	load(name, &a, &b, &c);
	avg = calc(a, b, c);
	print(name, a, b,c, avg);
}
In my early days of coding, I would remind myself of the difference between the two by declaring string buffers as "arrays":

Code:
void load(char name[], int *z, int *b, int *t)
{
	printf("Name");
	gets(name);
	printf("enter 3 values ");
	scanf("%d %d %d", z, b, t);
}

void print(char name[], int x, int y, int z, float avg)
{
	printf("%sn", name);
	printf("The 3 numbers are %d  %d  %d\n", x, y, z);
	printf("the avg is %f\n", avg);
}
Good luck!
__________________
Rich
Custom Wooden Case Builder
Overclock.net Mod of the Month

Last edited by Spotswood : 04-02-09 at 06:15 AM
Spotswood is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 10:58 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.10884 seconds with 8 queries