Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 12-12-2005, 06:47 AM   #1 (permalink)
SweetOne
Registered User
 
Join Date: Oct 2005
Posts: 6
SweetOne is on a distinguished road
Stuck on program

I keep getting an invalid selection printing. Also, I don't know how to code the "quit" option. Any advice would be greatly appreciated.
Thanks
PHP Code:
#include <stdio.h>

main()

{

int response;

 
char *lname[20]={'\0'};
 
char *fname[20]={'\0'};
 
int  *id={'\0'};
 
int  *gpa={'\0'};

  
FILE *pWrite;
  
FILE *pRead;


 
printf("\n\tStudent Files\n");
 
printf("\n1\tEnter Student Information\n");
 
printf("\n2\tPrint Student Information\n");
 
printf("\n3\tQuit\n");

 
printf("Select an option: ");
 
scanf("%d", &response);

  if (
response==1) {
    
    
printf("\nEnter first name: ");
    
scanf("%s"fname);
    
printf("\nEnter last name: ");
    
scanf("%s"lname);
    
printf("\nEnter student GPA: ");
    
scanf("%d", &gpa);
    
printf("\nEnter student id: ");
    
scanf("%d", &id);

  
pWrite=fopen("student_files.dat""a");

   if (
pWrite!=NULL) {

    
fprintf(pWrite"%s %s %d %d\n"fnamelnamegpaid);
    
fclose(pWrite);
  }

  else
   
     
printf("\nFile cannot be opened\n");

  }

 if (
response==2) {
   
   
pRead=fopen("student_files.dat""r");

   if (
pRead!=NULL)  {
   
printf("\nStudent Files\n");

     while (!
feof(pRead) ) {
       
fscanf(pRead"%s %s %d %d"fnamelnamegpaid);
  
     if ( !
feof(pRead) )
      
printf("\n%s %s\t%d\t%d"fnamelnamegpaid);
  } 
//end loop
  
 
 
printf("\n");
    

  }
  else
      
printf("\nFile not opened\n");

   }
  else
    
printf("\nInvalid Selection\n");

SweetOne is offline   Reply With Quote
Old 12-12-2005, 12:44 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
In this case I would've used a switch statement ie:
Code:
while(1){
    printf("\n\tStudent Files\n"); 
    printf("\n1\tEnter Student Information\n"); 
    printf("\n2\tPrint Student Information\n"); 
    printf("\n3\tQuit\n"); 

    printf("Select an option: "); 
    response = getchar() - '0';
    switch(response){
        case 1:
            printf("\nEnter first name: "); 
            ...
            break;
         case 2:
            pRead=fopen("student_files.dat", "r");
            ...
            break;
         case 3:
            printf("Exiting\n");
            return 0;
         default:
            printf("\nInvalid Selection\n");
    }
}
For your fscanf(stdin, "%d", &some_var); you might want to use the %f conversion, and find it produces better results.

For an exit check, just extend your if() checks, but as I mintioned earlier, for this I would rather use some form of switch() clause.
Code:
if(response == 3)
{
    printf("Exiting\n");
    return 0;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with copying backwards rogue Standard C, C++ 9 04-24-2005 05:39 PM
I'm Stuck Help verdell32 Standard C, C++ 2 11-02-2004 03:06 PM
C++ Deadlock Detection Program Help... coolsc81 Standard C, C++ 2 10-26-2004 07:14 AM
Help on starting new program B00tleg Standard C, C++ 21 10-17-2004 01:58 PM


All times are GMT -8. The time now is 12:33 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting