Thread: A beginners problem.

  1. #1
    Registered User
    Join Date
    Mar 2010

    A beginners problem.


    I have been beating my head against this problem for some time now, and figured I would see if i could get some fresh eyes, the code is:

    int main(){
            cout << "Enter as many words as you would like: ";
            vector<string> Words;
            string x;
            while(cin >> x){
            //How many words were entered
            int i = 0;
            int Count = Words.size();;
            const int n = Words[i].size();
            while(i != Count){
                    cout << n << endl;
    return 0;

    Pretty simple, just working no this functionality before i add what needs to be done with the concept. Right now it should be reading into a vector, using variable Count to count how many entries were made and uses this in the loop to determine when to stop.

    As of right now it does stop, however, although variable 'n' is const it does rely upon i changing. I can COUT i within the while function to verify that i was actually counting up (yeah i got that desperate)..but for some reason the result of n never changes..for instance..:

    [Spazmotic@localhost Chapter3]$ ./Exercise3-4
    Enter as many words as you would like: a ab abb abbb
    1111  <--- result

    I do know that since it is scoped it and looping there's no way for it to back and read the n definition again...but any way to fix it is beyond me

    I have tried declaring the variable 'n' right within the while loop but it Seg Faults as soon as i hit end of file..

    Any suggestions / Road signs?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    At the moment you declare the const variable n, its value is set and unchangeable. Just because you used the variable i in determining what to store in n at that point has no bearing in what n is later in the code as your loop increments i. n will not change because all it understands is that it holds a value. It doesn't understand how it got that value (the "equation" used to calculate it) and how to therefore magically alter itself based upon varying values of i.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2010

    That was an incredibly concise and helpful answer, thank you very much. I was under the assumtion that as there no actual value stored into 'n' but an equation, that every time 'n' it was called, it would run the equation. But the real way it works sounds much less like 'n' came from hogwarts

    I have no idea why I thought defining a constant within a loop would be a good idea, and why I did not realize this would cause a problem.

    Thank you again sir

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Beginner's Problem With First Funtion
    By Ohrange in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2007, 06:59 PM
  5. A beginner's problem
    By NewToC in forum C Programming
    Replies: 5
    Last Post: 11-21-2002, 05:20 PM