Strings and functions

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
void function();


void function()
{
    // Declaring a string object
    int hitPoints=10;
    int strike;
    string strl1 = ("#include <iostream>");
    string strl2 = ("using namespace std;");
    string strl3 = ("int main()");
    string strl4 = ("{");
    string strl5 = ("cout << fireStrike;");
    string strl6 = ("return 0;");
    string strl7 = ("}");
    string str1;
    string str2;
    string str3;
    string str4;
    string str5;
    string str6;
    string str7;
    
    cout << "Write a c++ Program to cast a fire strike \n";
    while (hitPoints != 0)
    {
        while(strl1 != str1)
        {
            getline(cin, str1);
            if(strl1 != str1)
                cout << "That is not correct, try again \n";
        }
        while(strl2 != str2)
        {
            getline(cin, str2);
            if(strl2 != str2)
                cout << "That is not correct, try again \n";
        }
        while(strl3 != str3)
        {
            getline(cin, str3);
            if(strl3 != str3)
                cout << "That is not correct, try again \n";
        }
        while(strl4 != str4)
        {
            getline(cin, str4);
            if(strl4 != str4)
                cout << "That is not correct, try again \n";
        }
        while(strl5 != str5)
        {
            getline(cin, str5);
            if(strl5 != str5)
                cout << "That is not correct, try again \n";
        }
        while(strl6 != str6)
        {
            getline(cin, str6);
            if(strl6 != str6)
                cout << "That is not correct, try again \n";
        }
        
        while(strl7 != str7)
        {
            getline(cin, str7);
            if(strl7 != str7)
                cout << "That is not correct, try again \n";
        }
        {
            
    						  srand((unsigned)time(0));
            strike = rand() % 10 + 1;
            cout << "You have cast a fireStrike at the monster \n";
            cout << "you hit a " << strike;
            hitPoints = hitPoints - strike;
            if (hitPoints <= 0)
                hitPoints = 0;
            cout << "The monster now has " << hitPoints << " left \n";
            str1.clear();
            str2.clear();
            str3.clear();
            str4.clear();
            str5.clear();
            str6.clear();
            str7.clear();
            
        }
        
    }
    
    cout << "You have killed a monster! \n";
    
}
int main()
{
    function();
    return 0;
}

  Put the code you need help with here.

I'm a college drop out, I don't really know the proper way to write c++. I'm just kinda messing around. sorry if my code makes you cringe. I'm trying to run a function that has strings in it. I want all the strings to be local. Im really not sure how to do it this is my best guess. can you help?


the error is at the bottom at line
function();
Hello supp,

A couple of observations.

1. the proto type on line 8 is not needed as you have defined your function before main. If you out the function definition after main then the proto type would be needed.

2. When I put your code in my IDE the function name "function" was a problem. I had to rename the function to make it work. When I tried to compile and run the program here it worked, but better to stay away from using the name "function".

3.
1
2
the error is at the bottom at line
function();
Did you forget to put the line number in, because I could not find any problems in the code for "function".

4. In the function function" you have several while loops to get you input. This is a good place to call another function like: void GetInput(std::string& strlx, std::string msg). The first parameter is passed by reference and represents the variables defined in "function" the second parameter is just a quoted string passed to "GetInput" that can be used in a std::cout to tell the user what to enter. This way you can get rid of all the while loops in "function" and make a more generic while loop in "GetInput" to do the same thing. A second alternative is to have "GetInput" return a string and take one parameter being "msg". Either one will work.

Hope that helps,

Andy
Handy Andy wrote:
2. When I put your code in my IDE the function name "function" was a problem. I had to rename the function to make it work. When I tried to compile and run the program here it worked, ...

Naming a function "function" shouldn't be a problem. My guess is that it clashed with std::function (http://www.cplusplus.com/reference/functional/function/). This wouldn't have happened if using namespace std; wasn't used.

Handy Andy wrote:
... but better to stay away from using the name "function".

I agree, but only because it's undescriptive.
 My guess is that it clashed with std::function (http://www.cplusplus.com/reference/functional/function/). This wouldn't have happened if using namespace std; wasn't used

So is this an instance where using namespace std gives 'silent' name clashes as Thomas1965 was enquiring a few days back?
http://www.cplusplus.com/forum/general/214265/#msg998769
Topic archived. No new replies allowed.