C++ :: Create 1 Variable That Accept Any Type Of Values?
Aug 24, 2013Can I create 1 variable that accept any type? And can I give it the NULL value too?
View 14 RepliesCan I create 1 variable that accept any type? And can I give it the NULL value too?
View 14 RepliesI want to fix this code. The program must accept 7 values or stop it when you type s. The code not only couts the element, but also some other random numbers.
Code:
#include<iostream>
using namespace std;
int main() {
int numb[7];
int i;
char s;
for (i=0;i<=6;i++)
[Code] .....
How to change an enum type variable to a string type variable?
View 2 Replies View Relatedhow can I create a program that will accept 2 two integers. the user will also choose among the different operations:
1 for addition
2 for subtraction
3 for multiplication
4 for division
i'm wondering if this will work
string user;
remove(user+".txt");
class Program {
//Accept two input parameter and returns two out value
public static void rect(int len, int width, out int area, out int perimeter) {
area = len * width;
perimeter = 2 * (len + width);
[Code] .....
why is the static keyword required in the method signature for the rect() method. It will not compile if it is absent. why?
the same is true for this example:
class Program {
static void printvalues(params int[] passedin) {
foreach (var printthis in passedin) {
Console.WriteLine(printthis);
[Code] ....
This code won't compile without the static keyword in the printvalues() method signature. why?
I am making a program which is going to print out a head image according to the input values entered by the user. Here is the code:
Code:
#include <iostream>
#include <string>
using namespace std;
void Sides()
// draws the sides to take proportion
[Code] .....
At first i tried to work program like:
Code:
cout << "plase enter which way you want to print out the head, with parted hair or bald." << endl;
cin >> headstyle;
if (headstyle != "bald" || headstyle != "parted hair" )
But it also had given the same mistake. The program compiles BUT; even if I put in the values bald or parted hair the program prints out "wrong input" then exits.
How can i make my program to accept only numerical values and gives a error notice if a character or a symbol is entered???
View 4 Replies View RelatedI would like to convert a float or double variable into unsigned char variable and back.
float number_float = 23.453f;
unsigned char* number_char = (unsigned char*)malloc(sizeof(float));
number_char = reinterpret_cast<unsigned char*> (&number_float);
float* number_float_0 = reinterpret_cast<float*>(&number_char);
I am not getting the same value back.. why?
Any way to create an "umbrella" type/class/struct of existing c++ types or windows types.
I want to address ~3 different structs with a single pointer and downcast but, they are not a part of the same hierarchy.
Is there a way that I could for example's sake, include a POINT and a RECT under some "umbrella" type so I can go like this:
// Working Variables
//---------------------------------------------------------
Umbrella* pUmbType1(NULL);
Umbrella* pUmbType2(NULL);
POINT ptExamp = { 0 };
RECT egRect = { 0, 0, 0, 0 };
// Typecast POINT and RECT to umbrella pointer
//---------------------------------------------------------
pUmbType1 = (Umbrella*)ptExamp;
pUmbType2 = (Umbrella*)egRect;
I want to do this, so I don't have to duplicate code for each type.
I'm attempting to pass a couple of variables over to my Item.cpp class, that is description and item_price. However under item.set(description,item_price), i get three errors. One under the . (period) saying : expected an identifier and two more under description and item_price stating that variable " xxx " is not a type name.
Main.cpp
#include <iostream>
#include "item.h"
using namespace std;
using namespace items;
int main(){
int n;
[Code] .....
So I am writing an assignment to Detect prime numbers and it works by Asking for how many values you are going to enter, and then saying "Enter value 1: "
Then you would input and it would calculate via for loop, and that part is working. However to make my program more foolproof, I devised a way for the user to be unable to "Break" the program by inputting characters or float values. Here is the code for that:
while(!(cin >> num)){ //num is some type (char, float, int etc.)
cout << "That is not at valid input, please try again" << endl; //"Error Message"
cin.clear();
cin.ignore(10000, '
'); //Clear and reset cin
cout << "Enter value " << n << ": "; //Re-Prompt User for input
//n is whatever value the for loop is on
}
and this code works fine, I was just curious about how i would turn it into a function. Preferably wiht the name: ValidateInput(Param1, Param2);
The Parameters of the function preferably would be the variable youre inputting and the message you want to prompt. So somehow i wish to have it so for the above example it would look like:
ValidateInput(num, "Enter Value " << n << ": ");
But I don't know exactly how to label either parameter part because I want it to work for chars, ints, floats etc. And I don't know what I want it to return if anything either.
I have code already and am looking to incorporate something new into it. I am trying to specify a bit size. A snippet of something similar I have is,
Code:
//header.h
int x; Code: //main.cpp
int func1(int a) {
x = a;
}
Is there anyway I can recast the variable x into another type? For example, if func2 had a char parameter instead, can I somehow make x become char type?
i need to create a new integer data type called BigInt to store a big big integer, which includes Dint(8 bytes) and Qint(16 bytes)
here is the hint/
typedef struct BigInt {
Int data[2];
}
How can i "scanf" and "printf" them????
void ScanBigInt(const char *format, BigInt &x)
if format is “%dd” -> input Dint, if format is “%qd”--> input Qint
void PrintBigInt(const char *format, BigInt x)
well i create a State.h class
#ifndef STATE.H
#define STATE_H
class State {
public:
virtual void handle_action() = 0;
virtual void update() = 0;
virtual void render() = 0;
};
#endif //STATE.H
What i'm trying to create is a simple State Manager for SFML! I created another class that inherits State.
#pragma once
#include "state.h"
class FirstState : public State {
public:
FirstState();
~FirstState();
void handle_action();
void update();
void render();
};
So the question is this, each state that i have will inherit the State class. However, i wanted to perhaps add each state object into a vector array. But i'm not sure as to what data type it be? I have a state manager class that will contain the vector.
What i want to do is this, each game state will create an object that will inherit functions from the state.h class. I want to store them all in a vector array, but each object is clearly named different. My curiosity was wondering, since all those different states inherit the State.h class, can i simply create a State Object std::vector<State> *states; that will contain all those different state objects?
[URL]....
I just compiled some code I've been working on at a different OS/compiler and realised that Code: sizeof(unsigned long) returns 4 in one pc and 8 in another.
I've heard that bytesize conventions for basic variables were not particularly "universal" before but this is the 1st time I've had a problem with it.
how do I make a typedef that clearly indicates to whatever compiler compiler I want u32 to be an 32bits unsigned and u64 to be 64bits?
I have two char variables, m_GPSOffset[13] and m_FileName[100]. When m_GPSOffset has a value assigned to it, say for instance +11:25:30. The first entry of the value, in this case +, is always stored in m_FileName. I am clueless on why this is occurring.
View 15 Replies View RelatedI'm a basic C++ programmer, and I am stuck on this problem. You work for a company that collects a set of numbers. The numbers are located in a data file named "./Data_File". The data file contains two columns. how do you count a certain number on the left column.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
#define DATA_FILE1 "./Data_File"
#define SUCEESS 0
#define FAIL 1
[code].....
The code below is supposed to be a program that allows the user to enter in 2 matrices and add them together, and gives an error when they're not the same dimensions.
The error I'm getting is on line 11 of MAIN.CPP and is saying "The Variable has incomplete type 'Matrix'".
MAIN.CPP
#include <iostream>
#include <vector>
#include <string>
#include "Matrix Class Input.h"
using namespace std;
int main() {
Matrix A,B,C;
int r,c=0;
[Code] .....
for ex: say i'm declaring two variables under int type and some 3 under char,my output should be lyk this: 2 variables in int and 3 var of type char...(input to the main program is actually another program where these 2 int and 3 char are defined).
View 1 Replies View RelatedWhat is wrong with assigning a reference variable to another reference variable of the same type? I guess I have not understood references very well.
1- In below code, the initialization list gets error because agent "object reference variable" cannot be initialized with a reference variable of the same type.
Code:
class Intention {
public:
Intention(Agent& agent,int Id, string name);
[Code] ....
In other places I have the same problem. In below code the assignment gets error (no overloaded function for assigning a reference to another reference?)
Code:
void Agent::setWorld(World& world) {
this->world = world;
}
//Note: this->world has a type of World&
2- In this other one, I want to assign reference of a vector member to a reference variable of the same type.
Code:
vector<Intention> intentions;
...
Intention& currentIntention = intentions[intentionsIterator]
I am trying to create a class type structure using struct instead of classes.
Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
struct myclass {
int * array;
int nelements;
[Code] ....
Guess what I am asking is using the pointer in the first code section better or is there another way'. I don't know about making the second code work.
Okay, I've been working on this Texture class that's going to be compatible with both SDL and OpenGL with the capability to create an array of textures within one class as opposed to multiple classes and such.
Now I've run into a slight issue if I want to return the value of a texture. There's two different types of textures: SDL_Texture, and a standard GLuint Texture. The problem I have is that one or the other is used, not both which is depending on whether or not the person is using OpenGL.
So when the user wants to get the texture, I need the ability to return either an SDL_Texture, or GLuint depending on whether or not OpenGL is being used.
I tried this with a template class, but it didn't work, but I'll post the code so you can see what I'm trying to do.
template <class Tex> Tex Texture::GetTexture(int TextureNumber)
{
if (TextureNumber > NumTextures || TextureNumber < 0)
return;
[Code]....
It basically just comes down to the last four lines of code. If the person is Using OpenGL return a GLuint, if the person is using SDL, return an SDL_Texture.
I would prefer to have the GetTexture function to be one function instead of two separate ones so I don't have to call a different function every time to check if I'm using SDL or OpenGL.
is there any variable type(or with another keyword) that we can change it's value in global scope?
View 5 Replies View Relatedint[ , ] Values; string[ , ] Formulas
ive tried this
string [,] formula=new string {[0,0]+[0,1]+[0,2]+[0,3]+[1,0]+[1,1]+[1,2]+[1,3],[2,0]+[2,1]+[2,2]+[2,3]+[3.0]+[3,1]+[3,2]+[3,3]};
C# type setting a char? I have tried setting as characters, as integers but nothing seems to work?
last try: char mchar = 'X'; // Character literal