Help please

This is a discussion on Help please within the C++ Programming forums, part of the General Programming Boards category; Hello I am a beginner and I need help with my code. The purpose of this program shall be like ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    6

    Help please

    Hello I am a beginner and I need help with my code.
    The purpose of this program shall be like this:
    The screen shall write "What is " followed by the question0. That answer shall be stored in the variable answer. Then the program shall write again on the screen "What is " followed by the question1 and so on. And that shall be stored in the variable answer. When all the questions have been asked the program shall summarize all the points and write how much you scored.

    The problem that I have got now, is how to get N working. So the question loop will work.


    Code:
    #include <iostream>
    using namespace std;
    
    int N;
    char question [] = "What is ";
    
     char fraga0[] = "question0";
     char fraga1[] = "question1";
     char fraga2 [] = "question2";
    int main ()
    {
      for ( N=0 ; N<5 ; N++ )
      {
    
         cout << question << fragaN << "\n";
         cin >> anwser;
      }
      return 0;
    }

  2. #2
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    I'm still a begnner myself and i'm not sure that this is right, but you have no variable answer, and there is no such thing as fragaN in your code.

    As i said, i'm still just a beginner myself. So don't spear me if i'm wrong...
    Last edited by beene; 01-02-2007 at 07:58 AM.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    6
    Quote Originally Posted by beene
    I'm still a begnner myself and i'm not sure that this is right, but you have no variable answer, and there is no such thing as fragaN in your code.

    As i said, i'm still just a beginner myself. So don't spear me...
    Yea I know. There is lots of code that is missing but I just need help with the N loop.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Code:
     using namespace std;
    
    int main( void )
    {
    	char *fraga[] = { "FIRST QUESTION", "SECOND", "THIRD" };
    
    	for ( int n=0; n<3; n++ )
    	{
    		cout << "What is: " << fraga[n] << "\n";
    		// ...
    	}
    	return 0;
    }
    Look into arrays!
    Also look into std::strings, or C++ style strings.
    Your variables also don't have to be global. Localise them into the scope of main..

    You can't loop through variables like how you did. It just doesn't make sense. You have to declare an array of variables and then shuffle through that. Think of it like this:

    var1 = []
    var2 = []
    var3 = []

    they're all single variables with no real relationship towards eachother. An array is different. It has a scope of elements assigned to it:

    array = {[][][]}

    if you can see what I mean by my diagram. So you can access each of those elements within the bounds of my curly braces by 'looking' at an element of the array, like so:

    Code:
    cout<< array[0];
    cout<< array[1];
    cout<< array[2];
    etc.
    Last edited by twomers; 01-02-2007 at 08:13 AM.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    6
    Thanks I think I understand it now.
    Last edited by danne123; 01-02-2007 at 08:12 AM.

  6. #6
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    This is how i would deal with it...

    Code:
    #include <iostream>
    using namespace std;
    
    char question [] = "What is ";
    
    enum questions
    {
        question0, question1, question2
    };
    
    int main()
    {
        for (questions N = question0; N<5; N++)
       {
            cout << question << N << "\n";
            cin >> answer;
        }
        return 0;
    }
    I havn't actually compiled and run this, so i'm not sure it will work, it's just off of the top of my head.

    EDIT:
    Wups, guess i'm a bit late, oh well...

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    danne123, I just edited my post. Take a read of it.

    beene, there's no need for enum's! That's jsut overcomplicating it. The OP is (probably) a beginner. A much simpler solution is what I put there.

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    6
    Now I need help again.
    This time is it about the program crashing all the time. For like 30 min ago the program worked just fine.

    So the question is for this time:
    1) Why is the program crashing
    2) The variable svar can only store 1 letter. How can you code so it can store a whole word instead.



    Code:
    #include <string>
    using namespace std;
    string val;
    
    int main()
    {
    cout << "Welcome to program Planets!";
    cout << "Valj mellan" << "\n";
    cout << "1: Svenska" << "\n";
    cout << "2: English" << "\n";
    cin >> val;
    
    if (val == "1")
    {
    
       //char startfraga[] = "Vad heter den ";
    
      char fraga0[] = "Sverige?";
      char fraga1[] = "Sverige?";
      char fraga2[] = "Tyskland?";
      char *fraga[] = { "FIRST", "SECOND", "THIRD", "THIRD", "THIRD" };
      char *svar;
    
        int n;
        for (n=0; n<5; n++)
        {
          cout << "Vad heter " << fraga[n] << "?";
          cin >> svar[n];
        }
    
        //test if these stuff works
        cout << "You have entered: ";
    
        for (n=0; n<5; n++)
        { cout << svar[n] << ", ";
        delete[] svar; }
    
    int asd;
    cout << "test";
    cin >> asd;
    
    return 0;
       }}

  9. #9
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    return 0; is out of scope

    EDIT:

    should be:
    Code:
    #include <string>
    using namespace std;
    string val;
    
    int main()
    {
        cout << "Welcome to program Planets!";
        cout << "Valj mellan" << "\n";
        cout << "1: Svenska" << "\n";
        cout << "2: English" << "\n";
        cin >> val;
    
        if (val == "1")
        {
    
           //char startfraga[] = "Vad heter den ";
         
            char fraga0[] = "Sverige?";
            char fraga1[] = "Sverige?";
            char fraga2[] = "Tyskland?";
            char *fraga[] = { "FIRST", "SECOND", "THIRD", "THIRD", "THIRD" };
            char *svar;
    
            int n;
            for (n=0; n<5; n++)
            {
                cout << "Vad heter " << fraga[n] << "?";
                cin >> svar[n];
            }
    
            //test if these stuff works
            cout << "You have entered: ";
    
            for (n=0; n<5; n++)
            { cout << svar[n] << ", ";
                delete[] svar; }
        } // here is where the closing brace should go if i'm reading this correctly
        int asd;
        cout << "test";
        cin >> asd;
    
        return 0;
    }
    The answer to your other question could be:

    Code:
    char *svar[];
    I think... I'm still a beginner as i stated earlier, so bare with me
    Last edited by beene; 01-02-2007 at 02:09 PM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    The delete[] looks rather out of place.
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "Welcome to program Planets!";
        cout << "Valj mellan" << "\n";
        cout << "1: Svenska" << "\n";
        cout << "2: English" << "\n";
    
        string val;
        cin >> val;
    
        if (val == "1")
        {
            //char startfraga[] = "Vad heter den ";
    
            char fraga0[] = "Sverige?";
            char fraga1[] = "Sverige?";
            char fraga2[] = "Tyskland?";
            char *fraga[] = { "FIRST", "SECOND", "THIRD", "THIRD", "THIRD" };
            string svar[5];
    
            for (int n = 0; n < 5; ++n)
            {
                cout << "Vad heter " << fraga[n] << "?";
                cin >> svar[n];
            }
    
            //test if these stuff works
            cout << "You have entered: ";
    
            for (int n = 0; n < 5; ++n)
            {
                cout << svar[n] << ", ";
            }
        }
        int asd;
        cout << "test";
        cin >> asd;
    
        return 0;
    }
    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

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    6
    Last two questions then I think the program is finished.

    1) The program skip the first question and jump directly to the second. Why? And how do you fix it?

    2) The program crash now when it comes to last question. If you lower n to a lower number then it won´t crash.

    Thanks for all the help I got earlier and now!

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "Welcome to program Planets! \n";
        cout << "Valj mellan" << "\n";
        cout << "1: Svenska" << "\n";
        cout << "2: English" << "\n";
    
        string val;
        cin >> val;
    
        if (val == "1")
        {
            //char startfraga[] = "Vad heter den ";
    
            char fraga1[] = "Merkurius";
            char fraga2[] = "Venus";
            char fraga3[] = "Tellus";
            char fraga4[] = "Mars";
            char fraga5[] = "Jupiter";
            char fraga6[] = "Saturnus";
            char fraga7[] = "Uranus";
            char fraga8[] = "Neptunus";
    
            unsigned short int x;
            x = 0;
    
            char *fraga[] = { fraga1, fraga2, fraga3, fraga4, fraga5, fraga6, fraga7, fraga8 };
            string svar[8];
    
                   for (int n = 1; n <= 8; ++n)
                        {
                cout << "Vad heter " << n << " planeten? ";
                cin >> svar[n];
                        }
    
    
            cout << "You have entered: ";
    
            for (int n = 1; n <= 8; ++n)
                       {
                cout << svar[n] << ", ";
               if (svar[n] == fraga[n])
               {x++;}
               else if (svar[n] != fraga[n])
               {x;}
                     }
            cout << "\n Grattis ni fick " << x << " ratt" << "\n" << "\n" << "\n";
    
            cout << "Foljande var fel:";
            for (int n = 1; n <= 8; ++n)
                      {
             if (svar[n] != fraga[n])
             {cout << svar[n] << ", ";}
                     }
    
    
        int asd;
        cout << "";
        cin >> asd;
    
        }
    
    
    
    }

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> for (int n = 1; n <= 8; ++n)
    Array indexes go from 0 to n-1, so your for loop is off by 1. Switch it to
    Code:
    for (int n = 0; n < 8; ++n)
    Then just output n+1 instead of n when outputting the question number.

  13. #13
    Registered User
    Join Date
    Jan 2007
    Posts
    6
    Thanks for all the help I got. I have really appreciate every tip.
    The program is working fine now.

    I got two general questions now. The first is, is there any way to count how many inputs of data a array got. Take this for example:

    Code:
            
    char *svar[] = { svar1, svar2, svar3, svar4, svar5, svar6, svar7, svar8 };
                               
                                   //i = sum (svar)        
    
    string fraga[i];
    
                   for (int n = 0; n < i; ++n)
                        {
                cout << "Vad heter de
    So every time I store a new data in the variable svar, the variable i will get bigger.



    The second question is how to turn of case sensitive? I read in some forums that some C++ programs have the option to turn it off but it is not recommended.
    But can you turn it off for a variable instead of the whole program?

  14. #14
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    It would be best to use a vector of strings to count the number of elements

    Code:
    #include <vector>
    
    
    [...]
    
    
    vector<string> vs_var;
    
    vs_var.push_back( "Can type some text" );
    
    string s_var = "Or you can input in like this";
    
    vs_var.push_back( s_var );
    
    cout<< "You can get the size of it like this: " << vs_var.size();
    Alas, you'll probably have to program the case sensitiveness (or lack thereof), yourself

  15. #15
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by danne123
    I got two general questions now. The first is, is there any way to count how many inputs of data a array got. Take this for example:

    Code:
            
    char *svar[] = { svar1, svar2, svar3, svar4, svar5, svar6, svar7, svar8 };
                               
                                   //i = sum (svar)        
    
    string fraga[i];
    
                   for (int n = 0; n < i; ++n)
                        {
                cout << "Vad heter de
    So every time I store a new data in the variable svar, the variable i will get bigger.
    Arrays don't work like that - its impossible to simply resize an array, or add an element on to the end. There are various work arounds though.

    1) statically create an array big enough to hold any number of values you wish to add, then create a variable which tracks how many elements there are. (Not a great option.. you usually end up either wasting space, or running out of space)

    2) Dynamically create an array, and create a variable which remembers its size. If you need to resize the array, create a new one, copy the old elements into it, update the size variable, and delete the old one. (This is the worst option IMO, plenty of things can go wrong)

    3) (The best and easiest option!) Use a vector from the C++ standard template library - a vector holds data in the same manner as an array, but uses member functions in order to resize, add elements, remove elements, search, etc. it also has a size() member function for you to use to find out how many elements there are.


    The second question is how to turn of case sensitive? I read in some forums that some C++ programs have the option to turn it off but it is not recommended.
    But can you turn it off for a variable instead of the whole program?
    I doubt you'll find any compiler that can do that. However, some IDEs such as MS Visual C++ can automatically change the case of some keywords and identifiers for you (A bit like spellchecking in MS Word), is this what you're after?
    Last edited by Bench82; 01-02-2007 at 05:44 PM.

Popular pages Recent additions subscribe to a feed

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