Thread: if any student is named Jackson, help please

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    if any student is named Jackson, help please

    Hi

    The code below is incomplete but still you can run it. There is some problem with it. When I have entered student #1's data it cycles through the loop automatically without asking for any data input. Please help me to solve the problem. Thanks.

    Code:
    // if_any_student_named_jackson_heights.cpp
    // check if any student is named Jackson Heights
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <iomanip>
    
    using namespace std;
    
    struct Student {char name[16]; int rollno; string sex; int age; float marks;};
    
    const int N = 5;
    
    Student stud[N];
    
    int main()
    {
    
            int i;
    
            cout << "enter the students' details below\n\n";
    
            for (i=0; i<N; i++)
            {
                    cout << "enter student #" << (i+1) << "\'s name: ";
                    cin.get(stud[i].name, 16);
                    cout << "enter roll no.: ";
                    cin >> stud[i].rollno;
                    cout << "enter sex: ";
                    cin >> stud[i].sex;
                    cout << "enter age: ";
                    cin >> stud[i].age;
                    cout << "enter marks: ";
                    cin >> stud[i].marks;
            }
    
    
            system("pause");
            return 0;
    
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do not use char, and do not use std::cin.get for strings. Use std::string and std::getline.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thank you, rags_to_riches, Elysia.

    @ Elysia: The problem is I have to use C-strings because this is what I'm being taught

    Okay. I think I have solved the problem in the code in my first post above. Now I face another problem. Please have a look on the output below. It should be "#2" instead. Where am I going wrong? Please help me. Thanks.

    Code:
    // if_any_student_named_jackson_heights.cpp
    // check if any student is named Jackson Heights
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <iomanip>
    
    using namespace std;
    
    struct Student {char name[16]; int rollno; string sex; int age; float marks;};
    
    const int N = 2;
    
    Student stud[N];
    
    int main()
    {
    
            int i; char name_search[20];
    
            cout << "enter the students' details below\n\n";
    
            for (i=0; i<N; i++)
            {
                    cout << "\nenter student #" << (i+1) << "\'s name: ";
                    cin.get(stud[i].name, 16);
                    cout << "enter roll no.: ";
                    cin >> stud[i].rollno;
                    cout << "enter sex: ";
                    cin >> stud[i].sex;
                    cout << "enter age: ";
                    cin >> stud[i].age;
                    cout << "enter marks: ";
                    cin >> stud[i].marks;
                    cin.ignore();
            }
    
            cout << "\n\nenter the name to be searched for: ";
            cin.get(name_search, 20);
    
            for (i=0; i<N; i++)
            {
                    if (stud[i].name == name_search)
                    {
                            break;
                    }
            }
    
            cout << "the student at #" << (i+1) << " in the record"
                 << " matches the entered name\n\n";
    
    
            system("pause");
            return 0;
    
    }
    OUTPUT
    Code:
    enter the students' details below
    
    
    enter student #1's name: jackson
    enter roll no.: 1
    enter sex: male
    enter age: 18
    enter marks: 99
    
    enter student #2's name: heights
    enter roll no.: 2
    enter sex: male
    enter age: 19
    enter marks: 100
    
    
    enter the name to be searched for: heights
    
    the student at #3 in the record matches the entered name
    
    Press any key to continue . . .
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You have to use strcmp to compare c strings. I would recommend you use c++ strings. Even if your teacher doesn't accept them, it's still good training.
    Last edited by Elysia; 06-09-2011 at 10:30 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Elysia View Post
    You have to use strcmp to compare c strings. I would reccomend you use c++ strings. Even if your teacher doesn't accept them, it's still good training.

    Thanks for the reply.

    If the teacher doesn't like then he will simply cross it!

    Isn't this possible to correct that "#3" issue without using strcomp function? I don't understand why the code generates, for example, "#3" instead of "#2". Please help me.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jackson6612 View Post
    If the teacher doesn't like then he will simply cross it!
    Well, like I said, it never hurts to try. And even if the teacher doesn't like it, you can make two versions, if only to familiarize yourself with C++ strings. You don't need to turn it in.

    Isn't this possible to correct that "#3" issue without using strcomp function? I don't understand why the code generates, for example, "#3" instead of "#2". Please help me.
    No. You are comparing two pointers, which would return equal iff the two were the exact same pointer. But you can have, for example, two strings not in the same memory location but with exactly the same contents.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by jackson6612 View Post
    Isn't this possible to correct that "#3" issue without using strcomp function?
    You could write your own function that does the same thing. Otherwise, no. And the function is called strcmp().
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks a lot, everyone.

    I have modified the original to make it work. But there is a point I couldn't understand.

    Code:
    if (strcmp(stud[i].name,name_search)==0)
    The if block is executed when the condition is evaluated to be true, that is "1" and "0" for false (the condition expression inside the parentheses is converted to bool type). So, why is "0" used above in the if condition expression for strcmp? Please let me know. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This has nothing to do with true or false. strcmp returns one of three different values: < 0, 0 and > 0. Check the manual for strcmp.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Elysia View Post
    This has nothing to do with true or false. strcmp returns one of three different values: < 0, 0 and > 0. Check the manual for strcmp.
    Thank, Elysia.

    I believe when it returns 0 it means the compared strings are the the same. What do other values, < 0 and > 0, mean? Please let me know. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Did you read the documentation?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Nope. Sorry.

    I have a class in next 5 mins and there is still so much to do!
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should always consult the documentation for something you don't understand.
    If you don't understand the documentation, then that is understandable, and then you have every right to ask a question about what you don't understand.
    But until then, I suggest you read the documentation. It's good training, and an essential skill.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with named pipe...
    By DamienCurr in forum C Programming
    Replies: 5
    Last Post: 03-27-2010, 09:49 PM
  2. Michael Jackson: dead at age 50
    By Sebastiani in forum General Discussions
    Replies: 78
    Last Post: 07-25-2009, 10:27 AM
  3. Named Pipes with .NET?
    By michl in forum C# Programming
    Replies: 2
    Last Post: 04-28-2003, 12:54 AM
  4. Michael Jackson is rad.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 02-08-2003, 08:33 PM