Thread: Help me! Im noob, 1st yr student in College

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    4

    Help me! Im noob, 1st yr student in College

    insert
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
        char word[4];
        char target;
        cin >> word;
        cin >> target;
        for (int x=0; x<5; x++)
        {
            if (word[x]==target)
            {
               cout << "yes" <<endl;
            }
            else
            {
               cout << "no" <<endl; 
            }
        }
        
        system ("pause");
        return 0;
    }
    ----

    output of this is:
    qwert (I input)
    q (I input too)
    yes
    no
    no
    no
    no

    -----

    I want 1 yes only if the target character is in the array but 1 no only if it's not in the array. Hope somebody can answer me, thanks.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you only want a single message printed, you might consider removing the print statements from the loop.

    One approach would be to have a flag that can be updated in the loop. When the loop is complete, you can print a message depending on the status of that flag.

    [edit] It's considered bad manners to cross-post your problem all at once. To understand why, you might want to read this: http://www.catb.org/~esr/faqs/smart-questions.html
    Last edited by Matticus; 01-10-2013 at 09:36 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Print after the loop. Of course, this means that you would say, use a boolean variable as a flag to record whether the target character has been found in the array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    char word[4];
    ...
    for (int x=0; x<5; x++)

    You also need to sort out buffer overruns, and the fact that "qwert" won't fit into 4 characters (3 actually, if you allow for the \0 marking the end of the string).

    Also cin >> singlechar;
    is problematic in that it will probably end up being the \n at the end of the previous input.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    a few questions to get you headed in the right direction:

    1. does the loop need to continue to look at the entire array after you found the first occurrence?
    2. does for (int x=0; x<5; x++) loop through the same number of items as are contained in char word[4]?
    3. what happens if someone enters more characters than the capacity of char word[4]?

    bonus question: how could you word the title of your post better to make it more descriptive, and actually encourage people to help you?

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    4
    insert
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
        char word[4];
        char target;
        int hit;
        hit=0;
        cin >> word;
        cin >> target;
        for (int x=0; x<5; x++)
        {
            if (word[x]==target)
            {
                hit=1;
            }
        }
        if (hit==1)
        {
        cout << "yes" <<endl;
        }
        else
        {
            cout << "no" <<endl;
        }
        system ("pause");
        return 0;
    }
    I think this answer my question

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You still have to make the corrections mentioned in post #4. Please ask if you need further clarification on those point.

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    4
    1st, I dont know what is buffer overruns, that's why I call myself noob
    2nd, qwert is not 4 character, it is 5. But it will fit in word[4] since [4] is index, word[0] is 1 index so as word[1], so it has 5 index.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In C/C++, array indices start at zero and go to N-1.

    Therefore:

    Code:
    char word[4];  /* declare a character array with four elements */
    
    word[0] = 'a';  /* first element of the array */
    word[1] = 'b';  /* second element of the array */
    word[2] = 'c';  /* third element of the array */
    word[3] = 'd';  /* fourth element of the array */
    
    /*
    These are the only elements available.
    
    This is not allowed:
    
        word[4] = 'e';  // fifth element, but you only declared four!
    
    This goes past the boundaries of the array, hence an overrun
    */
    Also, if you're using 'C' style strings, you need space in the array for the "invisible" null character, which indicates the end of the string.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by klorcs View Post
    insert
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
        char word[4];
        char target;
        cin >> word;
        cin >> target;
        for (int x=0; x<5; x++)
        {
            if (word[x]==target)
            {
               cout << "yes" <<endl;
            }
            else
            {
               cout << "no" <<endl; 
            }
        }
        
        system ("pause");
        return 0;
    }
    ----

    output of this is:
    qwert (I input)
    q (I input too)
    yes
    no
    no
    no
    no

    -----

    I want 1 yes only if the target character is in the array but 1 no only if it's not in the array. Hope somebody can answer me, thanks.
    Replace
    char word[4];
    with
    std::string word;

    Don't forget to include <string>.
    This will eliminate buffer overflows.

    Replace
    for (int x=0; x<5; x++)
    with
    for (int x=0; x<word.size(); x++)

    This will prevent out-of-bounds access and remove the manual responsibility of knowing the size yourself.
    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
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by klorcs View Post
    1st, I dont know what is buffer overruns, that's why I call myself noob
    2nd, qwert is not 4 character, it is 5. But it will fit in word[4] since [4] is index, word[0] is 1 index so as word[1], so it has 5 index.
    Your thinking is incorrect.
    If the declaration has [4] then there are only 4 indexes. One of those needs to contain he nul-terminator, leaving room for only a 3 character string in indexes [0], [1], and [2].

    Besides, it make no sense to go up to any fixed limit in the for loop every time. If I enter a two-letter word, then you only want to check two characters. Thus a better loop would be:
    Code:
    for (int x = 0; word[x] != '\0'; ++x)
    Line 11 is still very dangerous though. You aren't stopping the user from typing more than 3 characters into your buffer. If the user types 100 characters then they will most likely crash your program. Either use setw I think it is, or std::string.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick help for a college student :/
    By basbalinholywoo in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2009, 08:58 PM
  2. first year college student needs help!
    By stuartclancy in forum C++ Programming
    Replies: 3
    Last Post: 01-16-2006, 11:05 AM
  3. Newbie college student
    By jat421 in forum C Programming
    Replies: 3
    Last Post: 02-05-2005, 06:07 PM
  4. Newbie college student :)
    By jat421 in forum C Programming
    Replies: 15
    Last Post: 01-22-2005, 09:22 PM
  5. Freshman college student needs help
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 10-06-2001, 01:47 AM