Like Tree2Likes
  • 1 Post By Salem
  • 1 Post By Elysia

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

This is a discussion on Help me! Im noob, 1st yr student in College within the C++ Programming forums, part of the General Programming Boards category; insert Code: #include <iostream> using namespace std; int main () { char word[4]; char target; cin >> word; cin >> ...

  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
    2,370
    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 08:36 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    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.
    iMalc likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    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
    2,370
    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
    2,370
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    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.
    Elkvis likes this.
    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,304
    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, 10:05 AM
  3. Newbie college student
    By jat421 in forum C Programming
    Replies: 3
    Last Post: 02-05-2005, 05:07 PM
  4. Newbie college student :)
    By jat421 in forum C Programming
    Replies: 15
    Last Post: 01-22-2005, 08:22 PM
  5. Freshman college student needs help
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 10-06-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21