Thread: Noob to programming need help with a project

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Question Noob to programming need help with a project

    Hello, well as the title states im pretty new to programming, and I need help with a project I was given in class..

    Code:
    //preprocessor directives
    #define _CRT_SECURE_NO_DEPRECATE
    #include<stdio.h>
    
    void display (int skateBd, int longBd, int scooter, int ripStick, int bicycle);		//Funtion prototypes go here (minimum three user defined functions)
    int MostInStock (int skateBd, int longBd, int scooter, int ripStick, int bicycle);
    void restock (int skateBd, int longBd, int scooter, int ripStick, int bicycle);
    
    
    int main ()
    {
    int skateBd;	//declare 5 variables for the quantities of units (skateboard, longboard, etc.)
    int longBd;
    int scooter;
    int ripStick;
    int bicycle;
    int id,qty;
    
      skateBd =0;	//initialize all 5 variables to 0
      longBd =0;
      scooter =0;
      ripStick =0;
      bicycle =0;
    
      {
    	FILE *myPtr;							//declare pointer to a file
    
    	myPtr=fopen("records.txt","r");		//connect to the file
    
    	fscanf(myPtr, "%d %d", &id, &qty);	//get first pair of numbers from the file
    	
            
    	while(id!=-9999)
        {
    		printf("ID is %d, Qty is %d\n", id, qty);	//print each pair of numbers on the screen
    
    		if (id==1)		//do the comparisons witht the id here (use if/ese or switch)
    		{
    			skateBd=skateBd+qty;
    		}
    		else if (id==2)
    		{
    			longBd=longBd+qty;
    		}
    		else if (id==3)
    		{
    			scooter=scooter+qty;
    		}
    		else if (id==4)
    		{
    			ripStick=ripStick+qty;
    		}
    		else if (id==5)
    		{
    			bicycle=bicycle+qty;
    		}
    		
    
    	fscanf(myPtr, "%d %d", &id, &qty);	//get next pair of numbers from the file
        }
    	
    	fclose(myPtr);				//close the file
      }
    	//call display function
    	//call mostInStock function
    
    	//use if/else or switch here for return id from mostInstock
    
    	//call restock function
    	return 0;
    }  
    
    //function definitions go here (minimum 3 user defined functions)
    AS you can see I am not done with the bottem yet, only because when I run the program it triggers a Break. As I stated before I am new to this and have no idea whats wrong.

    By now everyone is probly laughing at how easy this looks, but all I can ask is for some help.


    Also my teacher gave me this and said to paste it where the source code is found so the file can be found easy.
    I just saved it as "records.txt" on notepad and pasted it in the are where the source code was.

    The records.txt file
    PHP Code:
    3 466
    5 983
    2 121
    1 194
    4 372
    -193
    -766
    -458
    -369
    2 12
    -9999 
    Once again I am a noob and if im not doing any thing right as for posting up something im VERY SORRY.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should check that fopen actually opened a file. If myPtr is NULL, then there's no point in trying to read from that file.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    Be easy on me now, how might I go about doing that?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm hoping you can translate "if" into C. "If myPtr is NULL". (Remember that "is" is "==".)

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    It still fails...
    Debug Assertion Failed !
    Expression: (stream!=NULL)

    and it says its located on the first fscanf .....
    Last edited by Wheelsonbus; 02-24-2009 at 10:54 PM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your latest code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Wheelsonbus View Post
    It still fails...
    Debug Assertion Failed !
    Expression: (stream!=NULL)

    and it says its located on the first fscanf .....
    You are (almost certainly) still trying to read from a file that didn't open (or is NULL for some other reason - perhaps you used one instead of two equal signs in the if-statement to check if the pointer is NULL?)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  3. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  4. importing to project ( noob help )
    By MystWind in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2005, 03:43 AM