Page 1 of 3 123 LastLast
Results 1 to 20 of 54
  1. Default Basic C++ problems


    For my assignment, I'm trying to make a program that stores my name as a variable and then displays "Hello! My name is <whatever value is stored in the variable". The problem is that my program won't function properly.

    What I have so far:
    Code:
    #include iostream
    using namespace std;
    
    int main () {
    
    string my_name = "Jonathan Chiou";
    
    cout << "Hello! My name is " << my_name;
    
    return 0;
    
    }
    What I get:
    Line 9: error: #include expects "FILENAME" or <FILENAME>

    What am I doing wrong, and what does this error mean?

  2. Certified Pimento Bi Male
    IGN: xxxxFenixR
    Server: Bera
    Level: Mix
    Job: Severals
    Guild: None
    Alliance: Nada
    Farm: Wut?
    venezuela

    Default Re: Basic C++ problems


    C++ right? if i remember correctly C++ Doesn't have the "String" Variable type. Try using Char instead.

    Know what nvm it does recognize it, its #Include <iostream> with the "<>"

  3. Default Re: Basic C++ problems


    That worked, thanks.

  4. Default Re: Basic C++ problems


    A couple of pointers. Pun not intended.

    1. You need to put brackets (< and >) around each library inclusion.
    2. std::string is part of of the <string> library so you should probably include it.
    3. return 0 is entirely optional in C++ but required in C.
    4. Try to avoid using namespace directives (using namespace std) as in the future you could mess something up without knowing. Try using namespace declarations instead (e.g. using std::cout;)

  5. Spirit of the Arrow Bi Female
    IGN: MariettaRC
    Server: Windia
    Level: 200
    Job: Bowmistress
    Guild: KoopaForce
    Alliance: KoopaEmpire
    Nebraska

    Default Re: Basic C++ problems


    To explain the error, it basically means "we're expecting this line to have this specific thing but it doesn't have it."

  6. Default Re: Basic C++ problems


    Bumping this thread cause I don't want to start a new thread.

    I have this code:
    Code:
    #include <iostream>
    #include <iomanip> // for fixed and setprecision
    #include "assn.h" // for is_vowel function
    using namespace std;
    
    int main () {
    
    int i = 0;
    string word = "regal";
    char ch = word.at(i);
    int wordsize = word.size();
    int syllables = 1;
    
    while (i < wordsize) {
        if (is_vowel(ch)) {
        cout << ch << " is a vowel." << endl;
       
            if (i > 0) {
            syllables = syllables + 1;
            }
        }
         
        else {
        cout << ch << " is not a vowel." << endl;
        }
       
        ch = word.at(++i);
    }
    
    cout << "Syllables: " << syllables; //ignore this
    
    return 0;
    
    }
    For references:

    No, I cannot change:
    #include <iostream>
    #include <iomanip> // for fixed and setprecision
    #include "assn.h" // for is_vowel function
    using namespace std;

    The grading program will check to see if I specifically have this header. It will not take anything else.

    The error I get is:

    std :: out of range.
    what(): basic string at

    I figured out that this is caused by i < wordsize, as if I have i < wordsize - 1, I do not get this error; however, it will only analyze "r e g a".
    The is_vowel function is a function provided by my profs for this assignment. If needed, I can post the code for the function.

    So, my question is, how do I prevent this error from happening while having my program analyze all the letters of any inputted word?

  7. Certified Pimento Bi Male
    IGN: xxxxFenixR
    Server: Bera
    Level: Mix
    Job: Severals
    Guild: None
    Alliance: Nada
    Farm: Wut?
    venezuela

    Default Re: Basic C++ problems


    Try using == or <= Wordsize. I recommend being more precise with == since you really don't have a range you want the program to recognize before stopping but a exact number. Although i always forget if while is the one that evaluates if the condition its true or false, so == may or may not stop prematurely.

  8. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: Basic C++ problems


    Your problem is that you are using i before checking it, at the bottom of the loop.

    Normally this loop would look like
    Code:
    i = 0;
    while (i < wordsize)
    {
      ch = word.at(i);
    ... if vowel ... blah blah...
      i++;
    }
    I don't know if you're supposed to know "for" loops yet, but if you do, the canonical form would be:
    Code:
    for (int i = 0; i < wordsize; i++)
    {
      ch = word.at(i);
      .... checking ch ...
    }

  9. Default Re: Basic C++ problems


    I know for loops, it's just that I prefer to use standard while loops.
    That looks much neater than my solution of branching into a break if i == wordsize -1, so I'll use that instead.

  10. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: Basic C++ problems


    That's a matter of personal taste, I suppose. 'for' is just as standard as 'while'.
    Personally I prefer to use 'for' when the loop is a basic progression from 0 to N-1, because it is clear at a glance that that is what the loop does.
    When you write "while (i < N)" it implies, to me, that there is some more complicated logic to i. That depending on the logic inside the loop, i might go back, or skip forward more than one, for example.

    (Of course, the language doesn't enforce either. I've seen some pretty crazy code in 'for' loops.)

  11. Default Re: Basic C++ problems


    Another note, .at() has bound checking since it throws std::out_of_range compared to [] so it might end up slower in the long run.

  12. Default Re: Basic C++ problems


    Used [] instead of (), it seems to working better so far.

    I'm running into another problem with my code.
    (Different code, but it's part of the same assignment.)

    Code:
    #include <iostream>
    #include <iomanip> // for fixed and setprecision
    #include "assn.h" // for is_vowel function
    using namespace std;
    
    int main () {
    
        string wordfile;
        int wordcount;
        int lettercount = 0;
        //int size = wordfile.size();
        int sentencecount = 0;
        int i = 0;
        char ch = wordfile[i];
        while (cin >> wordfile) {
            ++wordcount;
            lettercount = lettercount + wordfile.size();
            
            if (ch == '?' || '.' || ';' || ':' || '!') {
                ++sentencecount;
                ++i;
            }
            
            else {
                ++i;
            }
            
        }
        
        cout << wordcount << endl;
        cout << lettercount << endl;
        cout << sentencecount << endl;
        
    
    return 0;
    
    }
    What I'm trying to with this one is to determine the number of words, letters, and sentences there are in a document.
    Words and letters works fine if there isn't any other code, but once I implement my code for sentence count, it starts to go wrong from there.

    If I take out my code for sentence counting/displaying it, and run the program with text file that says "I like food.", it will display:
    10
    3
    Which is what I want.
    But if I include my sentence counting code in my program (see the code I posted above), I get:
    64
    10
    3
    I notice that the second and third values correctly display word count and letter count, but they're being displayed in the wrong position. Also, where did that 64 come from?
    How is the code I have created for sentence counting affecting the other code?

  13. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: Basic C++ problems


    That's not how you do an or.
    Must write
    Code:
    if (ch == '?' || ch == '.' || ch == ';' || ch == ':' || ch == '!')
    or, even clearer
    Code:
    if ((ch == '?') || (ch == '.') || (ch == ';') || (ch == ':') || (ch == '!'))
    C is quirky in that the way you wrote it compiles without warning, but it does not do what you think it does.

    Also, in your loop, you advance i, but do not recompute ch.

  14. Default Re: Basic C++ problems


    After each iteration, wouldn't ch plug in the new value of i every time?

    EDIT: After fixing my or statements, I get:
    64
    10
    0

  15. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: Basic C++ problems


    No.
    You didn't define ch to always be wordfile[i]. C++ doesn't even have an easy way to do that, except as a macro, which is very highly frowned upon. You could use a pointer (char*) instead of an index, if you've learned how to do that.

  16. Default Re: Basic C++ problems


    Haven't learned indexes yet. In order for ch to always be defined as wordfile[i], can I just define it before the if statement like:
    Code:
            ch = wordfile[i];
            if (ch == '?' || ch == '.' || ch == ';' || ch == ':'||
                ch == '!' ) {
                ++sentencecount;
                ++i;
            }
    And is there a reason why this text is affecting wordcount and lettercount even though they aren't determined by the same variables?

  17. Orbital Bee Cannon
    IGN: SaptaZapta
    Server: Kradia
    Level: 275
    Job: Hero
    Guild: Matriarchy
    Alliance: Peaceful

    Default Re: Basic C++ problems


    1. Yes. And that's not a definition, that's an assignment. You give it a value.
    2. Does your input file have empty lines after the line of text?

  18. Default Re: Basic C++ problems


    Yeah, an empty line always appears after the text whenever I save it.

    EDIT: Went to talk to my prof, ended up finding out that my program is only reading the 1st letter of every word. Edited my program, and problem solved.

  19. Default Re: Basic C++ problems


    Code:
    while (cin >> wordfile)
    That's an infinite loop.

    Code:
    if (ch == '?' || '.' || ';' || ':' || '!') {
                ++sentencecount;
                ++i;
            }
    Why not use a switch statement? Or even, std::ispunct

  20. Default Re: Basic C++ problems


    Professors don't want me to using switch, and haven't learned std:: ispunct.
    The point of the cin function is to read the document word by word, so it isn't infinite as long as a text document is inputted

  21.  

Bookmarks

Posting Permissions

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