Can someone please help me?
I am creating a program to calculate the cost of excess minutes with two cell phone companies. I am reading my data in from a file. This is the data from my file.

2      32
 3      45
 1      36
 3      15
 1      30
 1      32
 2      29
 5      59

and this is my code

/* File: cell.c
 * Team: TCC
 * Date: February 18, 2010 */

/* Two cell phone companies provide the same amount of free minutes and charge differently for excess minutes. This program calculates the cost of excess minutes per month for
 * each cell phone company. */

#include <stdio.h>

/* Function declarations */
int JOG_minutes(float minutes);
int HOLO_minutes(float minutes);


main()
{ /* Declarations */
  float min; /* Input for minutes */
  float sec; /* Input for seconds */
  float input; /* Input of minutes & seconds */
  float excess_min; /* minutes + seconds */
  int JOG_min; /* rounded JOG minutues invoked by function */
  int JOG_min_total; 
  int HOLO_min_total; 
  int HOLO_min; /* rounded HOLO minutes invoked by function */
  float cost_JOG; /* monthly cost with JOG */
  float cost_HOLO; /* monthly cost with HOLO */

        /* Obtain input for minutes & seconds */
        printf("Please enter the excess amount of time.\n");
        printf("Enter minutes, seconds: ");
        input = scanf("%f %f", &min, &sec);


/* Service provider - Jog */

                while (input != EOF)
		{
			/ * Converts input into decimal form */                       
                        if ( sec > 9 ) /* When seconds more than one digit */

                                { excess_min = (min) + (sec * 0.01); }

                        else /* When seconds is only one digit */

                                { excess_min = (min) + (sec * 0.1); }
			
			/* Invoke function to round of each line of input */
			JOG_min = JOG_minutes(excess_min);

			/* Sum all rounded calls */
			JOG_min_total = JOG_min + JOG_min_total;

			/* Update loop */
			input = scanf("%f %f", &min, &sec);

		}


			/* Calculate cost of all excess calls with JOG */
			cost_JOG = (JOG_min_total) * (0.05);


/* Service provider - Holo-T */

                while (input != EOF)
		{
			/ * Converts input into decimal form */                        
                        if ( sec > 9 ) /* When seconds more than one digit */

                                { excess_min = (min) + (sec * 0.01); }

                        else /* When seconds is only one digit */

                                { excess_min = (min) + (sec * 0.1); }
			
			/* Sum all calls, (non-rounded) */
			HOLO_min_total = excess_min + HOLO_min_total;

			/* Update loop */
			input = scanf("%f %f", &min, &sec);
		}

			/* Invoke function to round off the sum of all calls */
			HOLO_min = HOLO_minutes(HOLO_min_total);


			/* Calculate cost of all excess call with HOLO */
			cost_HOLO = (HOLO_min) * (0.07); 


/* Print Results */
	printf("The monthly prices for the 2 service providers are as follows:\n");
	printf("Jog: %f\n", cost_JOG);
	printf("Holo-T: %f\n", cost_HOLO);


} /* End main "/
	




/* Function for rounding minutes for JOG */

int JOG_minutes(float minutes)

/* Given: Excess minutes

   Returns: Excess minutes rounded to the nearest minute */

{
 int total;

   while(minutes >= 0) /* Calculates rounded number if greater than or equal to zero */

   { minutes = minutes + 0.5; }

        return minutes;

}

/* Function for calculating minutes for HOLO */

int HOLO_minutes(float minutes)

/* Given: Sum of all excess minutes

   Returns: The sum rounded to the nearest minute */

{int total;


        while (minutes >= 0)

        { minutes = minutes + 0.5; }

          return minutes;
}

I get two errors when compiling:
cell_draft.c: In function âmainâ:
cell_draft.c:38: error: expected expression before â/â token
cell_draft.c:67: error: expected expression before â/â token

What am I doing wrong?

Recommended Answers

All 15 Replies

Looks like you have a space after your / and before your * -- it won't parse the comment correctly like that so it's trying to evaluate your comment as a statement.

I think there is some problem with your formatting. Write your if else like this

if(  condition )
{
      // Some code
}
else
{
    // Some code
}

This is how I wrote the while loop in my editor

while (input != EOF)
                {
                        if(sec > 9 )
                        {
                                excess_min = (min) + (sec * 0.01);
                        }
                        else
                        {
                                excess_min = (min) + (sec * 0.1);
                        }

                //      JOG_min = JOG_minutes(excess_min);

                //      JOG_min_total = JOG_min + JOG_min_total;

                        input = scanf("%f %f", &min, &sec);

                }

and this compiled perfectly

There's nothing wrong with writing the if/else blocks that way. Once you hit the if() it's going to look for an opening brace or a statement the whitespace shouldn't matter.

Look at the colors of the two comment lines within the code tags, they are black, not green.

your problem in line 38 and 67 is.... only your remarks.

you put space in your remarks / * should be /*

thank you jonsca! the program compiled with no errors. however, when I ran the program, I am still having issues.

Please enter the excess amount of time.
Enter minutes, seconds: 2 3

after my input of "2 3", a blank space remains and it seems as if the program has stopped. do you know what seems to be the problem?

Well, scanf might not be the right option. I had thought you said you were reading in from the file anyway.

yes, I am reading in from a file.
what may be another option?

Using fgets to get a line from the file and using sscanf (string scanf) to get the two numbers you want. Your code is a little confusing about what you are setting out to do. I assume for each set of 2 points you want to get an output from one company and an output from the other?

So the fgets would drive the while loop and you would feed the numbers to one set of functions and then the other. Next set of 2 numbers same thing.

Edit: Here's an example of fgets with a file. Your string isn't nearly as long so you'd only need an array of 10-11 characters (EDIT: I had gone up and checked the file again it was wider than I thought)

the code is not reading a file because there  is no FILE *fptr; 

how about if you add some exit_flag in your loop.

int exit_flag = 0;

while (exit_flag = 0)

...exit_flag = 1;

while (exit_flag = 0)

should be while(exit_flag == 0) but I don't know if that helps the OP.

thanks for the correction. ==

try to see the value of input it has an EOF?. can you give us the code with reading your input file?

I found a big part of the problem. It's actually in your HOLO minutes and Jog Minutes functions. Minutes is presumably a positive quantity you pass it into those methods and say "as long as minutes is greater than or equal to zero, add 0.5 minutes" I don't think that's what you want. If you can use math.h you can use floor and ceiling functions as appropriate.

in my class, I don't think we learned about fgets yet.
the type of file I am reading the input from is cell.dat and when I run it I would run it as
a.out < cell.dat
and yes, for each input, I want to get an output from 2 different companies.
what other way may I do this?
is my while loop okay?

The while loops in your functions are definitely not fine. They are locking your program up. You need to re-evaluate your strategy for rounding (see my last post). Your main while loop might be ok but you should still combine those two while loops in main() into one.

I'm honestly not sure how fgets would work with the redirect. I assume it would work fine. You should ask your instructor about it because that fgets/sscanf is a powerful combo. See how the scanf works for you but check it's return value it should be equal to the number of items "scanned" (so in your case would be 2 for each line).

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.