Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cromulent

macrumors 604
Oct 2, 2006
6,810
1,100
The Land of Hope and Glory
well, like i said, i should have the book here in less than a week. but til then, i think i see the problem with my code, or at least i hope so.

it looks like i'm scanning for text, but shouldn't i be scanning for an 'int' in my switch statement?

The variable text is an int. You can check for anything in a switch statement. I just used an int in my example. You could have used a char if you wanted.

Your switch statement looks fine except for the lack of breaks. If you want it to keep looping then you need to put a loop in. The switch statement is a control statement, directing the flow of code depending on some input or variable. It is not meant to keep looping otherwise it would be called a loop.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
The variable text is an int. You can check for anything in a switch statement. I just used an int in my example. You could have used a char if you wanted.

Your switch statement looks fine except for the lack of breaks. If you want it to keep looping then you need to put a loop in. The switch statement is a control statement, directing the flow of code depending on some input or variable. It is not meant to keep looping otherwise it would be called a loop.

thanks. i'll try to fix it. (been a while since my last programming class :eek: )
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
well i made it work a little better.

i took out the switch statement, and put a while loop like this:

while (scanf ("%i", &text) != 5)

and then i made all the cases 'if' statements

but now it won't exit, even if i put in 5
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
That's because you are misunderstanding scanf. It returns an int which is the number of items successfully read. So when you enter a single int it will return 1. Always.

You need to do something like

Code:
do
{
scanf ("%i", &text)
}
while (text!=5)
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
You need to post your code to get help :).

yeah i know. the computer i'm programming on isn't connected to the internet right now though.

That's because you are misunderstanding scanf. It returns an int which is the number of items successfully read. So when you enter a single int it will return 1. Always.

You need to do something like

Code:
do
{
scanf ("%i", &text)
}
while (text!=5)

thanks. but if i type in '12345', it still won't close.

anyways, i can live with it not closing for now.

can i read from a file, and save the data to a variable?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
yeah i know. the computer i'm programming on isn't connected to the internet right now though.



thanks. but if i type in '12345', it still won't close.

anyways, i can live with it not closing for now.

can i read from a file, and save the data to a variable?

If you type "12345<return>" then the value scanned by scanf into text will be 12345, not 5. And yes, you can use the standard C functions to open and read a file: do a Google search/read a book.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
If you type "12345<return>" then the value scanned by scanf into text will be 12345, not 5. And yes, you can use the standard C functions to open and read a file: do a Google search/read a book.

but you said that scanf will scan the number of characters, so i put in 5 characters.

well i can open a file and all, but how do i read it and store the info as floats? would i use fread?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
but you said that scanf will scan the number of characters, so i put in 5 characters.

well i can open a file and all, but how do i read it and store the info as floats? would i use fread?

No, I said scanf would return, as an int, the number of entities it successfully converted. If you are scanning using scanf("%i",&text) and type 5 then scanf will return 1 (as 1 entity has been converted) and the value in text will be 5. If you type 12345 then scanf will still return 1 (as 1 entity has been converted) and the value in text will be 12345.

As I suggested: Google for C file reading.
 

Cromulent

macrumors 604
Oct 2, 2006
6,810
1,100
The Land of Hope and Glory
but you said that scanf will scan the number of characters, so i put in 5 characters.

well i can open a file and all, but how do i read it and store the info as floats? would i use fread?

do
{
int = scanf ("%i", &text)
}
while (int!=5)

I think that is what robbie means, I'm still learning myself so not 100% sure.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
do
{
int = scanf ("%i", &text)
}
while (int!=5)

I think that is what robbie means, I'm still learning myself so not 100% sure.

No I meant what I typed :p

The value returned by scanf is the number of entities converted: it's not the number you typed at the command line. The number you typed is stored in the variable text (well, more accurately at that memory location). So if you want to exit when 5 is typed you need to check that the value of text is not 5.

And I don't think you can have a variable called int either: that's a reserved word.
 

Cromulent

macrumors 604
Oct 2, 2006
6,810
1,100
The Land of Hope and Glory
The value returned by scanf is the number of entities converted: it's not the number you typed at the command line. The number you typed is stored in the variable text (well, more accurately at that memory location). So if you want to exit when 5 is typed you need to check that the value of text is not 5.

Yes I know. That is why I changed the variable in the while loop to the int returned by scanf rather than the text variable meaning the loop relates to the number of numbers rather than the number itself.

Have I understood that correctly?

And I don't think you can have a variable called int either: that's a reserved word.

I know :). But I couldn't be bothered to make a name up for it. Int just specifies it is meant to be an int :). Maybe I should have just called it X or something.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Yes I know. That is why I changed the variable in the while loop to the int returned by scanf rather than the text variable meaning the loop relates to the number of numbers rather than the number itself.

Have I understood that correctly?

I don't think so: your loop will never exit. In the code scanf can only ever return 0 or 1 as there is only 1 entity being scanned for: it can either be sucessfully converted or not. The OP wants the loop to exit when 5 is entered. That will never be the value returned by scanf, rather the value of the entity scanf converts and stores at the destination address. Perhaps this will help:

Code:
int convertedEntityCount=0;
int userEnteredValue=-1;
do
{
 print("Please enter in integer, 5 to exit\n");
 convertedEntityCount = scanf("%i",&userEnteredValue);
 if (convertedEntityCount==0)
 {
  print("You did not enter an integer: please enter an integer\n");
 }
 else
 {
  // Handle the user choice here: it's in variable userEnteredValue
 }
}
while (userEnteredValue!=5);
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
I don't think so: your loop will never exit. In the code scanf can only ever return 0 or 1 as there is only 1 entity being scanned for: it can either be sucessfully converted or not. The OP wants the loop to exit when 5 is entered. That will never be the value returned by scanf, rather the value of the entity scanf converts and stores at the destination address. Perhaps this will help:

Code:
int convertedEntityCount=0;
int userEnteredValue=-1;
do
{
 print("Please enter in integer, 5 to exit\n");
 convertedEntityCount = scanf("%i",&userEnteredValue);
 if (convertedEntityCount==0)
 {
  print("You did not enter an integer: please enter an integer\n");
 }
 else
 {
  // Handle the user choice here: it's in variable userEnteredValue
 }
}
while (userEnteredValue!=5);

thank you for the example. i think i can work off of that.

now, how would i read in data from a file, and store it? the data type i want is float.

would i use fread?
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
ok, well i thought i had this working, but i don't. here is the code:

Code:
if (text == 1)
{
     printf ("enter miles ");
     scanf ("%f", &miles);

     FILE *file;
     file = fopen ("miles.txt", "r+");
     fscanf(file, "%.2f", &totalMiles);
     fclose(file);
     totalMiles += miles;

     FILE *file1;
     file1 = fopen("miles.txt", "w+");
     fprintf(file1, "%.2f", %totalMiles);
     fclose(file1);
}

now this will work while the program is running, but when i close it, the data in the file is wrong, and when i run it again, it starts over at 0 miles

any ideas?

btw, this isn't all the code of the program
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
Are you sure that you want %totalMiles in the sprintf? Looks very wrong to me...

well, what i want it to do is this:

ask user to put in today's miles
open the file "miles" and take the total from there
add today's miles to the total miles
then write this to the file.

then when i run it again, i have yesterday's miles, and i add today's miles to that.

so i have a running total in the file "miles"

so i still don't want %totalMiles in there?

EDIT: sorry, it's really &totalMiles
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
I think at a pure "what does the operator and function do" level the % makes no sense. The % is the modulus operator: it's not even unary. That should not compile. What do you think the % is doing?

Edit: there we go, see I didn't think you wanted a %. Now I think the & is wrong. You are providing the memory address of that variable. You probably want to print the contents of the variable. So you just want totalMiles.
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
Why have you got two file streams for the same file? You could simplify that quite a bit I think.

haha, you don't want to see the rest of the code then. i could use some help ya know ;)

I think at a pure "what does the operator and function do" level the % makes no sense. The % is the modulus operator: it's not even unary. That should not compile. What do you think the % is doing?

Edit: there we go, see I didn't think you wanted a %. Now I think the & is wrong. You are providing the memory address of that variable. You probably want to print the contents of the variable. So you just want totalMiles.

yep, you got it. i realized it when i was changing that. thanks! :)
 

twoodcc

macrumors P6
Original poster
Feb 3, 2005
15,307
26
Right side of wrong
Yep, your fscanf will not work. I don't think you can specify a width here. So it should simply be fscanf("%f"...

thanks, now it updates the file correctly, but there is still a problem.

everything works fine, but when i quit the program and then start it again, the data starts back at 0
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.