Thread: Help; New in C++

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    8

    Help; New in C++

    Sample input: /this is a comment
    Output: /Syntax error

    Sample input: //this is a comment
    Output:


    What I want to make is a program that will ask for an input, and then if I type / followed by a string, it will display /Syntax error.

    Now if I type // followed by a string, it will not display anything.

    I was using getchar to get the input. The if-statement (input=='/') is read, but not (input=='//').

    Yeah, Im kinda aware that my logic is wrong in this one, so I was hoping someone here would show me the right approach?

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, you'll need to use strings, and look at the first couple characters of the string (note that 'char' only holds one character).

    Show us some code and we'll help. And, if this is your first experience with strings, this may be a bit much to tackle (start out simple).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    Yes, Im pretty new in C++. Im fairly new in programming actually, so please be easy on me.

    Okay, I'll write the original code I've done in my programming class as I remember it, coz' I forgot to bring my notes.


    Code:
    #include<iostream.h>
    
    main () {
    
    char input;
    
    input=getchar();
    putchar(input);
    
    if (input=='/') {
    cout<<"/Syntax error";
    }
    else if (input=='//') {
    cout<<"  ";
    }
    return 0;
    }
    Im sure there's some lines missing or written wrong here coz I can't recall the last program that well, and Im aware that the logic is wrong as well, thanks for pointing out that char only holds one character, so that's probably why (input=='//') is ignored. And, the prof wanted the line cout<<"Syntax error"; so it seems he want the character / to be displayed first and then followed by this line.

    Sorry if it all sounds too confusing. English isn't my native language, but Im trying my best to make sense.

  4. #4
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    I noticed two things things right off the bat.

    first, don't use iostream.h. It's old and does not conform to the current C++ standard.

    Second, your main function needs to to have a return value. And in the case of C and C++, main always returns an int, so it should be int main()
    Last edited by dra; 08-03-2005 at 10:17 PM.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    Here is something to try...not sure if it works exactly, i am not at my compiler.

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main () 
    {
        string intput;    //declares string
        cin >> input;    //asks user for string
    
        if (input == "/") 
        {
            cout << "/Syntax error" << endl;
        }
        else if (input == "//") 
        {
            cout << "  " << endl;
        }
        
        return 0;
    }

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    that wouldn't work the way he wanted.

    @sivex: another problem: you're trying to read a string into a char. like Zach L. already said, a char can only hold one character. you'll need to use a char array or stl string as in quizkiwi's example
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    Quote Originally Posted by dra
    I noticed two things things right off the bat.

    first, don't use iostream.h. It's old and does not conform to the current C++ standard.

    Second, your main function needs to to have a return value. And in the case of C and C++, main always returns an int, so it should be int main()
    Oh okay, thanks. Im trying to get rid off that bad habit of not including int before main.
    Last edited by sivex; 08-03-2005 at 10:52 PM.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    Quote Originally Posted by major_small
    that wouldn't work the way he wanted.

    @sivex: another problem: you're trying to read a string into a char. like Zach L. already said, a char can only hold one character. you'll need to use a char array or stl string as in quizkiwi's example
    Hmm.. okay. So I need to use char array for it to work? How's that done? Like char input[30], or something like that?

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    Quote Originally Posted by quizkiwi
    Here is something to try...not sure if it works exactly, i am not at my compiler.

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main () 
    {
        string intput;    //declares string
        cin >> input;    //asks user for string
    
        if (input == "/") 
        {
            cout << "/Syntax error" << endl;
        }
        else if (input == "//") 
        {
            cout << "  " << endl;
        }
        
        return 0;
    }
    Thanks. I'll copy this code and try it when I get home.

    Uhm.. but I'd like to know.. what does the line using namespace std; do?

    And is it really string intput, or string input?

  10. #10
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Quote Originally Posted by sivex
    Hmm.. okay. So I need to use char array for it to work? How's that done? Like char input[30], or something like that?
    that's a C-style string.

    a string in C++ is declared like this:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(){
    
    string s = "Hello";
    
    
    cout << s;
    
    return 0;
    
    }
    Notice that in order to use a string, you need to include the <string> header.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can use the code provided to read in the string, but it won't work for your sample in the first post because it only reads in a single word. To fix this, learn how to use getline with the string class to get the entire line.

    The next problem is the algorithm you need to use to find out whether the string is correct or not. You don't want to compare the entire string. What you want to do first is make sure the string has at least two characters (otherwise it can't be "//"). Then check the first character of the string. If it is a '/', then check the second character of the string. If it is not a '/', then you display the error message. Try to convert that into code.

  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    Quote Originally Posted by dra
    that's a C-style string.

    a string in C++ is declared like this:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(){
    
    string s = "Hello";
    
    
    cout << s;
    
    return 0;
    
    }
    Notice that in order to use a string, you need to include the <string> header.
    Oh ok. I'll remember that.

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    Quote Originally Posted by Daved
    You can use the code provided to read in the string, but it won't work for your sample in the first post because it only reads in a single word. To fix this, learn how to use getline with the string class to get the entire line.

    The next problem is the algorithm you need to use to find out whether the string is correct or not. You don't want to compare the entire string. What you want to do first is make sure the string has at least two characters (otherwise it can't be "//"). Then check the first character of the string. If it is a '/', then check the second character of the string. If it is not a '/', then you display the error message. Try to convert that into code.
    Im not familiar with this getline yet, but I'll try to learn more about it.

    So, I must check if

    1. string should be more than 2 characters; otherwise display an error message?
    2. the first character if its /. If its not, display an error message?
    3. check the second character if its /. If all the conditions are satisfied, display the blank message? Is that correct?

    Correct me if I misunderstood anything.

  14. #14
    Registered User
    Join Date
    Jul 2005
    Posts
    9
    try this:
    Code:
    #include<iostream>//remove '.h', it might still work, but it's not a good habit
    #include<string>//strings just make life much easier.  let's use them
    
    int main () {//once again, a matter of compilers, but most want you to specify that main is an int
    
    string input;//the string class is much more fun than char arrays or chars
    
    cin.getline(input, 100);
    /*the above function reads from cin and stores it in input until 100
    characters have been read, or until there's a new line (pressing enter)*/
    
    
    if (input[0]=='/' && input[1]!='\') {
    
    /*checks if the first character in input is '/', but makes sure the 
    second char isn't another slash.  that would mean the wrong 'if' 
    was being used*/
    cout<<"/Syntax error";
    }
    else if (input[0]=='/' && input[1]=='/') {
    /*makes sure that both of the first two chars are slashes.  kind of hackish, but it works*/
    cout<<"  ";
    }
    return 0;
    }
    hope this helps

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    In order to be a valid comment (using your syntax) the string must have two slashes ('/') as the first two characters (C++ also accepts the /* */ style comments). In your scenario, there may or may not be more characters in the string, but it has to be at least two char long, and the first two char must both be slashes. In your syntax, valid comment strings can have spaces (and presumably other whitespace char as well) between words, as in your OP. Therefore to read a comment as an entire phrase, spaces and all, you need to read it using some istream method that doesn't ignore whitespace char such as spaces. The two that come to mind are get() and getline(). get() can be used to read in a single char or an entire string, spaces and all. The string version won't remove any terminating char from the input stream. getline() can only be used to input strings (there is one version for C style strings and another for STL strings) and it will remove the first terminating char in the input stream that it encounters.

    So, if you read the entire comment in as an entire string all at once, then the string must be of at length == 2, otherwise it can't have two leading slashes. The length need not be longer than 2, but it must be at least 2. Thus strings of length 1 are invalid comments by definition. Whether you read the entire comment in as a string all at once or char by char, the first two char you read in must be slashes. In strings that would be index 0 and index 1 are slashes. Using char input it would be the first two char you read in.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed