I am trying to write a program that accepts a dollar rate and hte accepts a list of amounts adn converts tehm to Shekels and prints in a list with the sums.
HEre si my code:
It is giving em lots of errors and I am having no luck. I think strtok is the main problem but not sure how else to do it.

/*
Requests exchange rate and hen a list of dollar amount.
It then prints in a table the dollar and shekel amount and adds them up
*/
#include <stdio.h>
#include <string.h>

void main ()
{
	/* defines maximum list of dollar amounts*/
	#define MAXLINE 100
	
	float rate;
	float *dollar[MAXLINE]={0}; /*starts the array with a 0 so the rest of the array will be set to 0*/ 
	int x=0;
	float sumDollars,sumShekels; 
	int i=0;
	char c;
	int spaceCount=0;
	
	/*starts the sums to 0*/
	sumDollars=0.0;
	sumShekels=0.0;

	printf("Please input the current dollar exchange rate");
	scanf_s("%f",&rate);

	printf("Please input dollar amounts to calculate into Shekels. Return will end the list.");
		
	/* reads the list of dollar amounts*/
	while (spaceCount<=MAXLINE) 
	{
		if ((c=getchar())==' ')
			spaceCount++;
		if ((c=getchar())=='\n')
	}

	for (i=0; dollar[i]=strtok(c," "), i++);

		printf("$ \t IS\n");
		printf("__ \t __\n");

	/* Prints the list*/
	for ( x=0; dollar[x]!= '\n'; x++) 
		{
			sumDollars=sumDollars+dollar[x];
			sumShekels=sumShekels+(dollar[x]*rate);

			printf("%5.2f \t %5.2f", dollar[x], dollar[x]*rate);
		}
	getchar();
}

Recommended Answers

All 6 Replies

line #38:
u are not using strtok() in the way u should.
If u want to continue to get the next token then u must pass NULL in the 1st argument to strtok() after making the first call to it with the actual string.

its like:

char *tok = strtok(str, " ");
//next token u can get by
tok = strtok(NULL, " ");

U are passing the input string all the time hence its giving the 1st token all the time.

read manual of strtok (type "man strtok" in linux)

lines 31-36: You are getting a character from the keyboard but tossing it into the bit bucket (not doing anything with it). You need to create a character array and save the characters in it.

>>strtok(c," "),

strtok() works on character arrays, not single characters. Passing variable c as the first parameter will not work because it is not a character array.

line #38:
u are not using strtok() in the way u should.
If u want to continue to get the next token then u must pass NULL in the 1st argument to strtok() after making the first call to it with the actual string.

its like:

char *tok = strtok(str, " ");
//next token u can get by
tok = strtok(NULL, " ");

U are passing the input string all the time hence its giving the 1st token all the time.

read manual of strtok (type "man strtok" in linux)

Sorry I didn't checked all of your code and directly went to the problem line.
Thanks to Ancient dragon for mentioning that.

Thank you both. I ended up giving up on the strtok and using scanf instead of getchar. But i will need later in my curse strtok. so Thank you anyways.
Do either of you have experience with makefile. I do not understand how you are supposed to fo them. Or do you know where there is a clear explanation of what to do. All I know is I need to put in -Wall.
Thanks

Thank you. HOpefully I willhave mroe luck with my next program I have to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.