error: ‘String’ does not name a type

I am getting an error stating that error: ‘String’ does not name a type and I do not know why as why I am getting this error message. The error indicates at new string on line 8. Any ideas why this is happening?

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
  // Example program
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string  gear [4] = new String []{"Armor", "Dagger", "Helmet", "Shield"};
    int num[4]= {4, 2, 3, 3};
    int den[4]= {12, 12, 12, 12};
    char choice;
    int keep;
   
    cout<<"P(Armor) = 4/12"<<endl;
    cout<<"P(Dagger) = 2/12"<<endl;
    cout<<"P(Helmet) = 3/12"<<endl;
    cout<<"P(Shield) = 3/12"<<endl;
    
    cout<<"Try again for more gear?"<<choice<<endl;
    if (choice == 'y'){
            cout<<"P("<<gear[0]<<"| A)= "<<num[0]-1<<"/"<<den[0]-1<<endl;
            cout<<"P("<<gear[1]<<"| A)= "<<num[1]<<"/"<<den[1]-1<<endl;
            cout<<"P("<<gear[2]<<"| A)= "<<num[2]<<"/"<<den[2]-1<<endl;
            cout<<"P("<<gear[3]<<"| A)= "<<num[3]<<"/"<<den[3]-1<<endl;
        }
    else if (choice == 'n'){
        return 0;
    }
     cout<<"You could earn a "<<endl;
     cout<<"1. Armor"<<endl;
     cout<<"1. Helmet"<<endl;
     cout<<"1. shield"<<endl;
     cout<<"Which one do you want to keep? "<<keep<<endl;
     
      cout<<"Try again for more gear?"<<choice<<endl;
    if((choice =='y')){  
        if ((keep = 1)){
            cout<<"P("<<gear[0]<<"| A)= "<<num[0]-2<<"/"<<den[0]-2<<endl;
            cout<<"P("<<gear[1]<<"| A)= "<<num[1]<<"/"<<den[1]-2<<endl;
            cout<<"P("<<gear[2]<<"| A)= "<<num[2]<<"/"<<den[2]-2<<endl;
            cout<<"P("<<gear[3]<<"| A)= "<<num[3]<<"/"<<den[3]-2<<endl;
        }
        else if((keep = 2)){
            cout<<"P("<<gear[0]<<"| A)= "<<num[0]<<"/"<<den[0]-2<<endl;
            cout<<"P("<<gear[1]<<"| A)= "<<num[1]-2<<"/"<<den[1]-2<<endl;
            cout<<"P("<<gear[2]<<"| A)= "<<num[2]<<"/"<<den[2]-2<<endl;
            cout<<"P("<<gear[3]<<"| A)= "<<num[3]<<"/"<<den[3]-2<<endl;
        }
        else if((keep = 3)){
            cout<<"P("<<gear[0]<<"| A)= "<<num[0]<<"/"<<den[0]-2<<endl;
            cout<<"P("<<gear[1]<<"| A)= "<<num[1]<<"/"<<den[1]-2<<endl;
            cout<<"P("<<gear[2]<<"| A)= "<<num[2]-1<<"/"<<den[2]-2<<endl;
            cout<<"P("<<gear[3]<<"| A)= "<<num[3]<<"/"<<den[3]-2<<endl;
        }
        
        else if(keep<1 && keep>5){
            cout<<"Invalid choice"<<endl;
        }
    else if ((choice = 'n')){
        return 0;
        
    } 
    cout<<"Try again for more gear? "<<choice<<endl;
    
     if ((choice = 'y')){
         if(num[0]==3 && num[1]==3){
            cout<<"You could earn a "<<endl;
            cout<<"1. Armor"<<endl;
            cout<<"2. Dagger"<<endl;
         }
         else if(num[1]==3 && num[2]==3){
            cout<<"You could earn a "<<endl;
            cout<<"1. Dagger"<<endl;
            cout<<"2. Helmet"<<endl;
         }
         else if(num[2]==3 && num[3]==3){
             cout<<"You could earn a "<<endl;
             cout<<"1. Helmet"<<endl;
            cout<<"2. Shield"<<endl;
         } 
         else{
            cout<<"You could earn a "<<endl;
             cout<<"1. Armor"<<endl;
            cout<<"2. Shield"<<endl;
         }
     }
    }
}
Last edited on
string gear [4] = new String []{"Armor", "Dagger", "Helmet", "Shield"};
Hello, this looks like a weird mix of C# or Java, and not C++.

An array of strings can be initialized like this:
string gear[4] = {"Armor", "Dagger", "Helmet", "Shield"};

No need for "new" (unlike languages like C#/Java, where is it necessary most of the time).

Edit: Also,


else if ((choice = 'n')){


You probably meant to do ==, not =.
== tests for equality.
= is an assignment.

This is wrong in lines: 37, 43, 49, 59, and 65.

Edit 2:


else if(keep<1 && keep>5){



Logically, a number cannot be both less than 1 and greater than 5 at the same time. It doesn't make sense.
You meant to use OR (||), not AND (&&).
Last edited on
Also, note that names are case-sensitive in C++. string is not the same as String.
Topic archived. No new replies allowed.