SilabGarza - RS Private Server  
Home Forum Play Register Store Market Highscores Mark Forums Read
Go Back   SilabGarza - RS Private Server > SilGar Media > Computers > Programming
Reload this Page Project


Project
Old
  (#1 (permalink))
Mr 223 is Offline
Moderator
image
 
Mr 223's Avatar
 
Posts: 4,021
Cookies Given: 456
Cookies Received: 438
Time Online: 224 d 18 h 58 m
   
Default Project - 05-28-2014, 06:40 PM

Basically, this project is just about knowing how to use the file print and file scan on our C programs. It seems like there should be no problem. However, for whatever reason, my program isn't reading the + and - characters correctly(?), so instead of executing the case '+/-', it ignores it and goes through the next loop. Despite that, the * and / are working fine. I don't know what the problem is...
I'd appreciate some help.

My code:
Code:
#include <stdio.h>
#define FILENAME "CommandsProj1.dat"

int main()
{
   char op;
   char init[2];
   int a,b;
   FILE *cmd;


   cmd=fopen(FILENAME,"r");
   fscanf(cmd,"%s\n",&init);
   printf("Created by: %s\n\n",init);

    do{
        fscanf(cmd,"%c %i %i",&op,&a,&b);
        switch (op)
        {
        case 'H':
            printf("\nH brings up this command help screen.\n"
                   "Q ends the program.\n"
                   "+ adds two integers and prints results.\n"
                   "- subtracts two integers and prints results.\n"
                   "* multiplies two integers and prints results.\n"
                   "/ divides two integers and prints results.\n\n");
                   break;
        case '+':
            printf("%d",a+b);
            break;
        case '-':
            printf("%d",a-b);
            break;
        case '*':
            printf("%i %c %i = %d\n",a,op,b,a*b);
            break;
        case '/':
            printf("%i %c %i = %d\n",a,op,b,a/b);
            break;
        case 'Q':
            printf("\nProgram has ended.\n");
            break;

    }
   }while(op!='Q');

   fclose(cmd);

   return 0;

}
File CommandsProj1.dat:
Code:
JC
H
+ 3 5
- 5 3
* 10 7
/ 90 10
H
* 8 10
+ 2 8
- 2 8
/ 7 2
Q
Output:



  
Old
  (#2 (permalink))
D Kai is Offline
<span style="color: black">Dark eyes of life.</span>
image
 
D Kai's Avatar
 
Posts: 3,362
Cookies Given: 458
Cookies Received: 373
Time Online: 116 d 12 h 6 m
Clan: Imperium Owner
   
Default 06-05-2014, 06:35 PM

If you're using a .dat use the fread() and fwrite(). fscanf() isent that good.
So, being a .dat, you're using a Binary file and displaying in the what is in the file as a txt file.

Change your define to .txt and it should work.

NOPE NOPE

Found the error its fscanf(cmd,"%c %i %i\n",&op,&a,&b);
you need the add the '\n' since it is a Char and it needs to be in the formated Input function fscanf();

Code:
#include <stdio.h>
#include <string.h>

int main(){
   char op;
   int a,b;
   FILE *cmd;
   cmd=fopen("commandsProj1.txt","r");
    do{
    	fflush(stdin);
        fscanf(cmd,"%c %i %i\n",&op,&a,&b);
        printf("%c %i %i\n",op,a,b);
        switch(op){
        case '+':
            printf("%i %c %i = %i\n\n",a,op,b,a+b);
            break;
        case '-':
            printf("%i %c %i = %i\n\n",a,op,b,a-b);
            break;
        case '*':
            printf("%i %c %i = %d\n\n",a,op,b,a*b);
            break;
        case '/':
            printf("%i %c %i = %d\n\n",a,op,b,a/b);
            break;
   		}
   }while(!feof(cmd));

   fclose(cmd);

   return 0;

}
cookie me

Last edited by D Kai; 06-05-2014 at 07:07 PM.
  
Old
  (#3 (permalink))
Mr 223 is Offline
Moderator
image
 
Mr 223's Avatar
 
Posts: 4,021
Cookies Given: 456
Cookies Received: 438
Time Online: 224 d 18 h 58 m
   
Default 06-06-2014, 04:17 AM

Quote:
Originally Posted by D Kai View Post
Spoiler
I had actually gotten it fixed already. The problem was that my fscanf was reading garbage, so I added an extra fscanf to store that garbage into a variable, gb, so that it would no longer mess with the program.

Thanks for the input.



  
Old
  (#4 (permalink))
D Kai is Offline
<span style="color: black">Dark eyes of life.</span>
image
 
D Kai's Avatar
 
Posts: 3,362
Cookies Given: 458
Cookies Received: 373
Time Online: 116 d 12 h 6 m
Clan: Imperium Owner
   
Default 06-06-2014, 05:54 AM

Quote:
Originally Posted by Mr 223 View Post


I had actually gotten it fixed already. The problem was that my fscanf was reading garbage, so I added an extra fscanf to store that garbage into a variable, gb, so that it would no longer mess with the program.

Thanks for the input.
Problem it has reading garbage its because it miseed the last invisible char, the new line char.

Also, fgetc and fgets are alot better. Alot better.
  
adsense code2.3
Closed Thread

Bookmarks

Project

Programming




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
adsense2.6
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 12:00 AM.





Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.