Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 23

Thread: Need a little help with my IA

  1. #11
    Join Date
    Jan 2009
    Posts
    2,404
    Rep Power
    0

    Default

    Quote Originally Posted by RickyRudy View Post
    Oh i figured it out I'm getting along slowly but surely..

    Funny thing.. I only needed one line..

    total+=grade[exno];

    Currently I'm trying to make a file.. The name of a person entered previously..
    So its like this

    FILE *report;

    report=fopen(filename, "a+");

    But i'm stumped on getting the .txt part for the filename.. So its just basically a file..

    And also.. If i have a variable initialized as a char.. But i'm using it to hold a string so.. Whenever i try to input a name with a space between it for example : Ricky Rudy
    The program skips the code after it..

    int AvG()
    {
    int studage, value, val, exno, exno1, total=0, grade[5], u, count=0 ;
    float aveg;
    char name[50], subject[30], Class[20], filename[50];

    printf("Please enter the name of a student.\n");
    scanf("%s", &name);
    printf("Please enter the class %s is in.\n", name);
    scanf("%s", &Class);
    printf("Please enter the age of %s.\n", name);
    scanf("%d", &studage);

    If i should enter the student name as something with a space it won't allow me to enter anything for the class.
    Is there any other way to fix that other than having an additional printf and scan f for the second name?
    That's because scanf treats whitespace as a input separator, so the next scanf value '&Class' takes what comes after the space, etc. Can't suggest a fix cuz I'm rusty at the little C I once did.
    Rooted OnePlus 2 64GB Ed, Android 5.1.1 OxygenOS ; on teifin' AT&T's network; Rooted ASUS Transformer TF101 w/ dock, Android 5.1 KatKiss; Laptop: ASUS X550C, 2.0GHzx2, 8GB, 512GB SSD, Kubuntu 15.10;
    Facebook page: Skeleville Technology Solutions

  2. #12
    Join Date
    Jan 2008
    Posts
    1,514
    Rep Power
    0

    Default

    Guess I'll use
    Code:
    printf("Please enter the First name then Surname name of a student.\n");
       scanf("%s%s", &name, &name1);
    unless someone has something better.

  3. #13
    Join Date
    May 2003
    Posts
    229
    Rep Power
    0

    Default

    Quote Originally Posted by RickyRudy View Post
    Yep skiing i got that

    strcpy(&filename, name);
    strcat(&filename, name1);
    strcat(&filename, ".txt");

    Got the full name for the text Thanks for the help
    Actually that's wrong, if "filename" is an array of char then it's value is the address of the first character in the array. The correct thing is

    Code:
    strcpy(filename, name);
    strcat(filename, name1);
    strcat(filename, ".txt");

  4. #14
    Join Date
    May 2003
    Posts
    229
    Rep Power
    0

    Default

    Quote Originally Posted by RickyRudy View Post
    Guess I'll use
    Code:
    printf("Please enter the First name then Surname name of a student.\n");
       scanf("%s%s", &name, &name1);
    unless someone has something better.
    I hate scanf so what I usually do is write my own input function. If you're serious about C, it might be useful for you to write your own input function also, but if you do you might want to finish your assignment first and then just write the function for fun.

  5. #15
    Join Date
    Oct 2005
    Posts
    745
    Rep Power
    0

    Default

    Quote Originally Posted by RickyRudy View Post
    Guess I'll use
    Code:
    printf("Please enter the First name then Surname name of a student.\n");
       scanf("%s%s", &name, &name1);
    unless someone has something better.
    You could also look at the gets() function. I do recall hearing that it is not recommended but it works.

    http://www.cplusplus.com/reference/c...y/cstdio/gets/
    3.14159265358979323846264338327950288
    4197169399375105820974944592307816406
    28620899862803482534211706798 pi 101

  6. #16
    Join Date
    Jan 2008
    Posts
    1,514
    Rep Power
    0

    Default

    Having a problem with this loop..

    I need it to check for a file if the file doesn't exist it prompts the user to enter the name of the file they want to read until they enter the right file name
    Code:
    int read()
    {
        char filename1[50];
    
    printf("Please Enter the name of the child whos report you would like to view.\nNo spaces between the names & This is case sensitive\n\nExample : RicardoMcdonald\n");
    scanf("%s", &filename1);
    strcat(&filename1, ".txt");
    
    FILE *fp=fopen(filename1, "r");
    while(fp==NULL){
     system("CLS");
        printf("There is no report on the student entered\n\n");
    
        printf("Please Enter the name of the child whos report you would like to view.\nNo spaces between the names, This is case sensitive\n\nExample : RicardoMcdonald\n");
        scanf("%s", &filename1);
    
    FILE *fp=fopen(filename1, "r");
    
        strcat(&filename1, ".txt");
    
    }
    
            system("CLS");
    
        printf("File has been found");
        system(&filename1);
        printf("File has been sucessfully open\n");
        fclose(fp);
    retmenu();
    }
    Currently using
    Code:
    int read()
    {
        char filename1[50], namee[10000];
    
    FILE *fpp;
        fpp=fopen("C:\\reportscreated.txt", "r");
    
    if(fpp == NULL)
    {
        printf("I'm sorry but for an unknown reason the text file which stores the names was not created\n");
    }else{
    while(fgets(namee,10000,fpp)!=NULL);
    {
    printf("%s", namee);
    }
    printf("\n");
    }
    
    
    printf("Please Enter the name of the child whos report you would like to view.\nNo spaces between the names & This is case sensitive\n\nExample : RicardoMcdonald\n");
    scanf("%s", &filename1);
    strcat(&filename1, ".txt");
    
    FILE *fp=fopen(filename1, "r");
    
    if(fp==NULL)
    {
     system("CLS");
        printf("Please try again, You will now be returned to the main menu.\n\n"); 
    error();
    }
        while(fp!=NULL){
    
            system("CLS");
        fclose(fp);
        printf("File has been found\nFile has been sucessfully open\nWhen done viewing file please close it to return to main menu\n");
        system(&filename1);
        
    retmenu();
    }
    }
    
    int error()
    {
        printf("There has been an error!!\n");
        retmenu();
    }
    Should i just stick with what I'm using currently? returning to the main menu would give the option to get back into this menu.

    Also how can i display things from a text file I have a list of names i would like to display and

    Code:
    while((fgets(name ,500, fpp)!=NULL);
    Only displays the last name on the list only.
    Last edited by RickyRudy; Jan 21, 2010 at 09:30 PM.

  7. #17
    Join Date
    Jan 2009
    Posts
    2,404
    Rep Power
    0

    Default

    First, there's at least one bug in the first code section. Check your pointer declarations . Next, there's alot of redundancy in that same first section. I suggest you use a do...while instead of repeating the same process outside and inside the loop. BTW, I'm loving how you unconditionally force people into entering a name . Finally, could you please properly indent your code? It'd make it so much easier to read. Both for you and us ...
    Rooted OnePlus 2 64GB Ed, Android 5.1.1 OxygenOS ; on teifin' AT&T's network; Rooted ASUS Transformer TF101 w/ dock, Android 5.1 KatKiss; Laptop: ASUS X550C, 2.0GHzx2, 8GB, 512GB SSD, Kubuntu 15.10;
    Facebook page: Skeleville Technology Solutions

  8. #18
    Join Date
    May 2003
    Posts
    229
    Rep Power
    0

    Default

    Quote Originally Posted by recursion View Post
    You could also look at the gets() function. I do recall hearing that it is not recommended but it works.

    http://www.cplusplus.com/reference/c...y/cstdio/gets/
    fgets is safer because you get to specify the maximum number of characters read (to prevent a possible buffer overrun).

  9. #19
    Join Date
    Jan 2008
    Posts
    1,514
    Rep Power
    0

    Default

    well to use fgets i'd have to change most of my code.. I found a way though

    scanf("%[^\ns", <variable>); Works perfectly just incase there's anyone else with such a problem

    What i need now is help with checking if a interger is a char

  10. #20
    Join Date
    Jan 2009
    Posts
    2,404
    Rep Power
    0

    Default

    You mean a letter right? Just check if it's between the ASCII values. 64 - 126 I think; don't quite remember exactly...
    Rooted OnePlus 2 64GB Ed, Android 5.1.1 OxygenOS ; on teifin' AT&T's network; Rooted ASUS Transformer TF101 w/ dock, Android 5.1 KatKiss; Laptop: ASUS X550C, 2.0GHzx2, 8GB, 512GB SSD, Kubuntu 15.10;
    Facebook page: Skeleville Technology Solutions

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •