C/C++ :: Getting Errors On String And Enum Variable Declarations?

Oct 13, 2014

I need to do this simple Battleship game project for my C++ class. Here is my current code so far:

#include <iostream>
#include <array>
#include <string>
using namespace std;
enum BShip{ EMPTY, BTLSHIP, HIT, MISS };
const int BSIZE = 6;
const int BTLSHIP_SIZE = 4;
class Battleship {
private:
array<array<Bship, BSIZE>, BSIZE> sea, hits;

[code]....

For some reason, I'm getting errors on the string and enum declarations.

View 10 Replies


ADVERTISEMENT

C/C++ :: Convert Global Declarations To Local Declarations

Mar 17, 2015

How can I convert these Global Declarations to only be Local?

#include <stdio.h>
#include <math.h>
int calculateArea(int num1, int num2);
double calculatePerimeter(double num3, double num4, double num5);

//Global Delcarations
float side1,side2,side3;
float area;
double perimeter;

[Code].....

View 8 Replies View Related

C/C++ :: Change Enum Type Variable To String Type Variable?

Aug 10, 2014

How to change an enum type variable to a string type variable?

View 2 Replies View Related

C/C++ :: Convert Enum To Text String

Jan 25, 2015

I want to convert an enum to a text string.

enum BREED {YORKIE,CAIRN,DANDIE,SHETLAND,DOBERMAN,LAB};

View 4 Replies View Related

C++ :: Using A String / Enum To Access Int Matrix?

Apr 2, 2012

Any way to use a string to access a specific item in a matrix of int[X].

I have a program which uses enums as iterators to reference a large amount of data. To select an item in the matrix, the user will enter a string, which is also an enum, which also must serve as an iterator for something in the matrix. Here is a toybox example:

#include <iostream>
#include <string>
using namespace std;
enum NyNumbers { First, Second, Third, Forth, LAST_VALUE };
int main(int argc, char* argv[]) {
int Matrix[LAST_VALUE] = { 1, 3, 7, 12 };

[Code] .....

The idea is the user executes the program by typing "./RUN First" to print out the first element in the MyNumbers array, "./RUN Second" to access the second, and so on. I can't use static numbers for the iterator. (i.e., "./RUN 1") I must reference by enum/string.

When run, the output of this problem is thus:

====================================================================
user@debian$ ./RUN Second
Matrix[ atoi(Second) ]: 1
user@debian$
====================================================================

BTW, if I change line 12 to this

====================================================================
cout<<Matrix[ argv[1] ]<<"
";
====================================================================

The error message is this

====================================================================
user@debian$ make
g++ -g -Wall -ansi -pg -D_DEBUG_ Main.cpp -o exe
Main.cpp: In function `int main(int, char**)':
Main.cpp:12: error: invalid types `int[4][char*]' for array subscript
make: *** [exe] Error 1
user@debian$
====================================================================

Which isn't unexpected. How to input the enum as argv[1]?

View 2 Replies View Related

C++ :: Read In String From File And Convert It Into Enum?

Dec 2, 2013

I defined

enum boundaryType_t {inside, inlet, outlet, wall, periodic};

now I have file written like this:

inside outlet

I want my program read the file and when encounter any enum type, instead of treating it as a string, I want the program store it in memory as a enum value.

How can I do that?

string s;
ifs>>s;
s // how to convert it to enum???

View 5 Replies View Related

C++ :: Converting Enum To String And Vice Versa?

Feb 25, 2014

It is working:

#include <iostream>
#include <string>
#include <vector>
#include <map>
const int ENUM_NOT_FOUND = -1; const std::string NEW = " ";
enum Day {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};

[code]....

Ouput with GCC 4.8.1:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Name a day: Friday
day = Friday

But the problem is that whenever I define a new enum, I have to define the << and >> overloads for the new enum again. Isn't there a way to template that as well, so that the << and >> overload needs to be defined just once? My atttempt:

template<typename Enum>
std::ostream& operator << (std::ostream& os, Enum en) {
return os << EnumConversions<Enum>::toString (en);

[Code] .....

fails to compile. I guess the problem is Enum is not known at compile time, even though it should be deducible during run time? Error mentions ambiguous overload for operator>>.

View 6 Replies View Related

C/C++ :: Uninitialized Local Variable Errors

Sep 7, 2014

I keep getting the "Uninitialized Local Variable" error. But for my code it's says it's the variable 'pay' in my Manager Function. This is the only error that is popping up.

I've tried setting pay to 0 but when I do, I get a bunch of external errors. I've also tried assigning pay to WeeklySalary like this:

double pay = WeeklySalary;

//Calculating pay for a company

#include <iostream>
#include <iomanip>
using namespace std;

//Function prototypes
double managerFunction();
double hourlyWorkerFunction();
double commissionWorkerFunction();

[Code] .....

View 10 Replies View Related

C :: Variable Or Field Declared Void And Other Errors

Mar 29, 2014

I'm creating a payroll program and here is my code

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define REPORTHEADING1 " Employee Pay Hours Gross Tax Net
"
#define REPORTHEADING2 " Name Rate Worked Pay Due Pay
}

[code]....

The errors I get are: variable or field PrintSummaryReport declared void in function voidPrintSummaryReport... cannot convert FILE to const car for argument to int printf(const char*...) which is for the following lines

Code:

printf(reportFile,REPORTLINEFORMAT1,fullName,payrate,regHours,gross,fedtax,
ssitax,netpay);
printf(reportFile,REPORTLINEFORMAT2,ovtHours,statetax,deferred);

View 7 Replies View Related

C/C++ :: Getting Variable Or Field Declared Void And Other Errors

Mar 28, 2014

I'm creating a payroll program and here is my code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define REPORTHEADING1 " Employee Pay Hours Gross Tax Net
"
#define REPORTHEADING2 " Name Rate Worked Pay Due Pay

[code]....

View 14 Replies View Related

C :: Simplify Complex Declarations Using Typedef

Jan 2, 2014

I have some complex declarations to simplify with typedef I have done a try

1. Code:
char (*x[10]) (int);
/* typedef char FUNC(int);
typedef FUNC *FUNC_PTR;
FUNC_PTR x[10]; */ Why we don't use * symbol in the last statement in front of FUNC_PTR?

2. Code:
int (*x(int))[5] I can't do this :/

3. Code:
float *(*x(void))(int);
/* typedef float *FUNC(int);
FUNC *x(void); */

View 7 Replies View Related

C++ :: Write A Loop Assigning Variable X To All Positions Of String Variable

Sep 8, 2013

I have to write a loop assigning a variable x to all positions of a string variable and I'm stuck. I don't have extensive experience with arrays and I'm also a bit confused about C-String. The problem is below.

"Given the following declaration and initialization of the string variable, write a loop to assign 'X' to all positions of this string variable, keeping the length the same.

char our_string[15] = "Hi there!";

(Please note this is a 'C-string', not C++ standard string.)"

View 5 Replies View Related

C++ :: Class With Enum And Constructor

May 29, 2013

I really confused with constructor (default constructor and constructor with parameters)

I coded this problem

and I worked almost, but I stock in constructor

Code:
class Tier {
public:
enum TIER_MAKE

[Code] ....

This is tier class and I have to finish constructor in class car (for simple, I skip detail code) -red things are the parts from class Tier

Code: Car()
: make(NULL), passengers(0), fuelcap(0.0), efficiency(0.0), tier(Tier::nexen)
{ }

[Code] ....

And someone said default constructor part has to be this

Code:
car( Tier::TIER_MAKE p_tiermaker = Tier::nexen )

//after i skip
but default constructor should be no parameter...? isn't it?

View 1 Replies View Related

C++ :: Getting Error Using Enum In If Statement?

Jan 19, 2014

I'm trying to make a simple C++ program in which the user must try to guess a number, if they guess too high it says "too high" and if they guess too low it says "too low".

I also decided to add a feature which allows them to select how many tries they would like to guess the number. I tried to make "tries" type an enum so if the user could not pick an invalid number but for some reason i cannot use it in an if statement.

here is the code and i am getting the first error on line 27:

Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int guess;

[Code]....

View 7 Replies View Related

C++ ::  Using Enum Defined In Class

Apr 1, 2013

I am trying to write a game in C++ with SDL, and I have a class that allows me to handle events. The class is actually really simple: It takes the SDL_Event, then 2 variables from 2 different enum to determine for which Event and which Key should be checked, and then a variable that will be modified if the event happens. Here is the class

EventParser.h
#include "SDL.h"
#include "SDL_opengl.h"
template<class T>

[Code]...

As of yet the variable only changes if the Left key has been released, it will be extended if the error has been solved.

Then, in my main.cpp file I define the Event and the EventParser as

SDL_Event event;
EventParser<float> ep;

And in a loop, the parseEvent function is called like this:
ep.parseEvent(event, ep.KeyUp, ep.LEFT, &xVariable);

However I get a linker error (not the first one I got when programming this game)
error LNK2019: unresolved external symbol "public: void __thiscall EventParser<float>::parseEvent(union SDL_Event,enum EventParser<float>::EventType,enum EventParser<float>::KbdKey,float *)" (?parseEvent@?$EventParser@M@@QAEXTSDL_Event@@W4EventType@1@W4KbdKey@1@PAM@Z) referenced in function _SDL_mainC:UsersPrideRageDocumentsVisual Studio 2012ProjectsSDL_TestSDL_Testmain.objSDL_Template

View 6 Replies View Related

C :: Enum - Incomplete Type Is Not Allowed

Sep 30, 2014

I have an enum like:

Code:
typedef enum mac_type_e{
STATIC_MAC,
BLACKLIST_MAC
} mac_type_t; and I want to use this type in a structure that's declared like:

Code:
typedef struct lan_mac_s {
UINT16 lanmacid;
enum mac_type_t lan_mac_type_pp;// user mac type per port, 20 mac_type_t array, 0 = static, 1 = blacklisted now,

The compiler tells me:

Code:
incomplete type is not allowed
enum mac_type_t lan_mac_type_pp;// user mac type per port, 20 mac_type_t array, 0 = static, 1 = blacklisted

But if I remove the preceeding "enum" keyword, it compiles fine.

View 2 Replies View Related

C++ :: Enum Across Files - Unresolved Error?

Feb 3, 2014

I defined

enum symbol {A, B, C};

in file A.h

and in file B.h, I #include "A.h" , but still get error of A , B and C unresolved when using them. WHy?

View 15 Replies View Related

C++ ::  switch Case With Enum Types

Jul 4, 2014

I'm trying to convert the enum type {PG, R, G, PG-13, etc.} to strings so i can use it in cout statement but no matter what i put inside switch(), the compiler keeps saying Error: expression must have integral or enum type. What am I doing wrong exactly?

Movie covert_rating(Movie r) {
switch (r) {
case PG:
return "PG";
break;
case R:
return "R";

[code]....

View 4 Replies View Related

C++ :: Initializing Enum Member In Constructer

Jul 25, 2012

I am learning OOP Console Programming and i got some error in my code;

Here is my code

#include <iostream>
using namespace std;
enum ch {CD,DVD}
class disk {
private :
ch choice;
[Code] ....

I want to compare the enum variable to char c, and cout CD when user press 'c' and cout DVD when user press 'd'. I got error like this :

error : expected ')' before 'c' at first line after public

Note : I must use enum to store value.

View 7 Replies View Related

C++ :: Get Value From String Containing Variable Name?

Nov 2, 2014

As written in the title, I want to be able to extract a variable value from a string containing the variable's name. I know one can use associative containers such as maps but is there another more direct way?

E.g.:

int variable = 5;
string str = "variable";

// how do I get the value of 5 out of the string containing the variable name?

If I am correct, this is called 'Reflection', correct me if I'm wrong.

I know C++ has no inbuilt 'Reflection' class or anything like that so I was wondering if there is a workaround for this kind of thing or is there a library out there which can do this? (that's if I have the name right).

I have found a library called Boost Reflection which sounds like it could do this but I just wanted to make sure that reflection is actually what I am talking about and whether C++ can do what I'm trying to do? I'm not sure how.

View 5 Replies View Related

C :: How To Handle Enum Checks In Function Arguments

Aug 31, 2013

As we know in C there is no checking if values passed to a function that takes enum are correct, that is if they have been defined in this enum. Example from Enums in C | Occasionally sane ([code] tags don't work on my fx 18.0.1 this is why I put in on pb): [URL] ......

Here c - How to check if an enum variable is valid? - Stack Overflow they say that common convention is add check whether value passed as the parameter is not bigger than the maximum value in enum. But how about situations when enum is composed of numbers from 1-20 and then from 500-510?

View 4 Replies View Related

C Sharp :: Test Enum Without Converting To Array First?

May 7, 2013

Is there a quick way to determine if a value is in an enum? Consider for example,

public enum GenderEnum
{
Unknown = 0,

[Code].....

I don't want to convert to an array if possible, just check the enum and get a yes/no answer.

View 1 Replies View Related

C :: How To Store First String Into A Variable

Feb 11, 2013

Im trying to write a program that reads in strings and decides if the 1st one is repeated. I cant figure out how to store the first string into a variable, and compare that variable to the rest of the inputted strings.

Code:

#include <strings.h>
#include <stdio.h>
int main () {
//Declared variables
int i;
}

[code]....

View 3 Replies View Related

C++ :: Manipulate A String Into Name Of A Variable?

Feb 1, 2013

The while loop is increased by i++ each time. In the commented line I want to express.When:

i = 1 , player1.cards[j] = random;
i = 2 , player2.cards[j] = random;
void cardScramble()
{
int random;
int i = 1;
while (i <= 4)

[code]....

I tried to define it or manipulate it as a string but didn't work.

View 2 Replies View Related

C++ :: IntelliSense - Expression Must Have Integral Or Unscoped Enum Type

Sep 6, 2014

So far I have the following code:

// Purpose: To write a program that displays the number of millimeters higher the current level the ocean level will be in
// in 5, 7, and 10 years.

# include <iostream>
# include <string>
using namespace std;
int main() {
float X = 10;
string Y="";

[Code] ....

But I get the following error message:

IntelliSense: expession must have integral or unscoped enum type

three times in a row for lines 25, 27, and 29 and I don't understand or know why?

In case the purpose does make sense here are the directions:

2.7: Ocean Levels

Assuming the ocean’s level is currently rising at about 1.5 millimeters per year, write a program that displays

•The number of millimeters higher than the current level that the ocean’s level will be in 5 years,
•The number of millimeters higher than the current level that the ocean’s level will be in 7 years,
•The number of millimeters higher than the current level that the ocean’s level will be in 10 years,

Output labels:

Each value should be on a line by itself, preceded by the a label of the form:

In X years the ocean's level will be higher by Y millimeters.

where X is the number of years (5, 7 or 10) and Y is the value you calculate.

View 16 Replies View Related

C++ :: Creating A Class Variable With A String?

May 12, 2013

I have defined a class in a header file; just the class, no templates involved. I have a program where I'm reading in data in string format. Each string consists of a word, a delimiter, and a variable name. Example:

cajun/mustard

I want to take that string and make it the variable name of that class type. It would be implemented along the lines of:

Code:
string str;
//read/process string here, get:
str = "mustard";
createName(str);
//pass string to creator function When the function is called, I should get the variable:
Class mustard;

Thing is, I'm not supposed to know beforehand what the variable names are, only that I create them as they are read in. It could be mustard, it could be Maynard_James_Keenan, it could even be bazinga.

My problem is, what do I do for createName()? I've looked into the concepts of pairing, Factory implementation, and maps, but I don't think they answer my question.

(P.S. if I run into the same variable name being read in twice, what steps can I take to make sure that a duplicate variable isn't created? Do I need to add in code, or does the compiler know to watch for multiple variables of the same name?)

View 6 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved