C++ :: Random Integer Must Be A Constant
Mar 18, 2014
//VALID:
const int CONSTANT=100;
int integerArray[CONSTANT]={ 0 };
//but after getting input let's say:
cin>>randomInteger;
int integerArray[randomInteger]; // This is invalid.
// VISUAL STUDIO 13 Says : randomInteger must be a constant; If so?
const int CONSTANT=randomInteger; //This is also invalid.
How to get user defined
//Input in a constant variable?
How to resolve this? I know dynamically allocation other than this.
I am using VISUAL STUDIO 13 ....
View 3 Replies
ADVERTISEMENT
Nov 20, 2014
This is my coding so far and I am confused to what the constant for array size is
//initialize arrays
string states[ARRAY_SIZE]={"Alabama", "Alaska", "Arizona"};
string capital[ARRAY_SIZE]={"Montgomery", "Juneau", "Phoenix"};
while (play again) {
//generate random index number
int index = rand () % _______
what goes after the rand () % ?
View 1 Replies
View Related
Jun 11, 2013
Write a program that plays the game of guess the number.the program chooses the number to be guessed by choosing an integer at random in the range 1-1000. The program then types 'i have a number between 1 and 1000,can you guess number? Then the player then types the first guess, the program responds.
View 1 Replies
View Related
Feb 20, 2015
Can distinguish between character constant and string constant giving examples
View 1 Replies
View Related
Dec 28, 2012
#include <iostream>
class Hello {
public:
void Test() {
[Code].....
As i know a non-constant member function cant be called inside a constant member function but how the above code has been compiled successfully and giving the expected result .
View 5 Replies
View Related
Aug 31, 2013
I want to make 10 random numbers thus making 10 random flips of a coin. I am getting 10 tails or 10 heads!
Code: #include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(int argc, const char * argv[])
{
[Code].....
View 4 Replies
View Related
Oct 25, 2013
I am trying to assign the integer value to unsigned char array. But it is not storing the integer values. It prints the ascii values. Here the code snippet
Code: uchar uc[100];
for(i=0;i<100;i++)
{
uc[i] = i;
}
The values which are stored in uc[] is ascii values.I need the integer values to be stored in uc[]. I tried to do it with sprintf. but the output is not as expected. if I print the uc[i] it should diplay the value as 0,1,2....99.
View 9 Replies
View Related
Jun 15, 2014
changing a 9 digit integer into a new 9 digit integer through simple mathematical operations. For example, I need to change 123456789 into the new digit 456123789. Sometimes I need to change a 9 digit integer into an 8 digit integer. An example is 789062456 into 62789456. I can ONLY use simple mathematical operations (addition, subtraction, multiplication, division and modulo).
View 4 Replies
View Related
Oct 15, 2014
Complete the function myitohex so that its converts 8-byte integer to string based hexadecimals.
Ex: printf("0x%s",myitohex(64,hex)); should printout "0x40" on the screen
char *myitohex(uint8_t i, char *result){
???
return result;
}
I wrote:
#include <stdio.h>
#include <stdint.h>
char *myitohex(uint8_t i, char *result){
*result = i + '0';
[Code] ....
0xp gets printed out which is wrong. I think *result = i + '0'; is wrong. What changes should i do on this row ?
View 4 Replies
View Related
Oct 23, 2013
line 27 and line 88 Im having a hard time figuring it out what the error is.
#include<iostream>
#include <cmath>
#include<algorithm>
[Code]....
View 2 Replies
View Related
Apr 10, 2013
why all strings are always constant?
View 2 Replies
View Related
May 20, 2014
Why pointer cannot be initialized with a constant like.
Code: int *p = 3000;
View 6 Replies
View Related
Jun 16, 2014
I have never seen anyone pass by const copy and there probably is a reason. I know that the compiler ignores top level const-ness of function arguments. There are functions which take arguments without manipulating those arguments return the result, for example the C Standard Library funcion double sqrt (double x). The function shouldn't modify it's argument, but it can since the argument isn't const.Take these two functions for example:
double square_root_1(double arg)
{
arg = 7; // we won't get the desired results
return arg * arg;
[code]....
So isn't it better to pass by const copy to make sure that you (or someone else) don't by accident modify the argument? The only disadvantage I see is that it makes the code too verbose.
View 10 Replies
View Related
Jan 20, 2013
What is the difference between:
const int testFunction() &
int testFunction() const
View 3 Replies
View Related
Aug 27, 2013
#include<iostream>
using namespace std;
class Student{
public:
int age;
int rollNo,marks;
string name;
void AddEntry();
[Code] .....
error: non-member function 'void Display(Student*, int)' cannot have cv-qualifier|
why and how can I solve it?
View 7 Replies
View Related
Apr 4, 2013
I heard that const shall be preferred over #define . So I start to change my program accordingly.
But then below error message occurs during compilation:
#include "common.h"
#include "definition.h"
#include "particle.h"
int main() {
Particle *p = new Particle();
[Code] .....
I guess the error occurs because, when the line 9 of particle.h (File 4) is compiled, value of const int dimension is not seen by the compiler.
View 6 Replies
View Related
Mar 14, 2013
I have a class that I'm going to use to store a category. Right now there are seven options, but there is the potential for a whole lot more in the future. So I started off by storing an integer as the private member. I then used static constants to define the numeric values to represent each category, then a set of static constant strings that corresponds to those numbers in case I need their actual names. Finally I set up some static functions to convert between the integer value and the string, and vice versa.
I'm not sure if this is the best way to go about this. For one it makes the categories names and designations unchangeable. I thought that storing them in a file would be a better option, but then i needed a container that is the equivalent of a constant.
I thought of defining a class to contain an int and the associated string. It would be designed so that it can only be initialized with both items. Then provide no functionality to change the contents. So I've basically created my own constant.
View 4 Replies
View Related
Aug 4, 2014
Why do I get the error 'rec' cannot appear in a constant-expression ?
I have the following definitions:
... string rec[6];
list<rec> musicList;
...
View 17 Replies
View Related
Dec 24, 2012
double &val = 66.6; //illegal
const double &val = 66.6; //legal
I was just doing some demo programs and came through the above concept but not able to identify what exactly the need of the above concept . what magic exactly const is doing in the second case ?
Where exactly we can use this concept in real time programming ?
View 4 Replies
View Related
May 20, 2013
Instead of using:
Code:
x=x+k
y=y+k
z=z+k
Is there a more elegant method of adding the same constant to many variables?
Something like: Code: (x, y, z) = (x, y, z) + k ??
View 5 Replies
View Related
Oct 23, 2013
I am trying to use libXl to output text from a C++ program to an Excel file. The problem is coming with this library function:
bool writeStr(int row, int col, const wchar_t* value, Format* format = 0)
Writes a string into cell with specified format. If format equals 0 then format is ignored. String is copied internally and can be destroyed after call this method. Returns false if error occurs. Get error info with Book::errorMessage().
If I give input as a string literal like "Hello World" it is displayed correctly. However, if I try to give input as a variable of type const char*, it displays garbage.
Following is my code. MyCompany::company is a QString.
const char* companyName = MyCompany::company.toStdString().c_str();
sheet->writeStr(4, 0, companyName, companyFormat);
View 3 Replies
View Related
Jan 8, 2014
I have defined a pointer ptr2 as follows:
const void * ptr2 ( new A(20) );
In this case I expect the data pointed by ptr2 must be constant. But when running the program, the data could still be changed successfully. Why ?
Below is the program:
#include <iostream>
using namespace std;
[URL] ....
class A {
public:
A( int ID ) {
cout << "--Constructor with ID=" << ID << endl;
this->ID = ID;
[Code] .....
The output:
--Constructor with ID=20
ID of ptr2 is 20
ID of ptr2 is 21 after changed!
-~Destructor with ID=21
End of main()
Process returned 0 (0x0) execution time : 0.002 s
Press ENTER to continue.
Question: What is the significance of "const" in const void * ?
View 19 Replies
View Related
Jun 5, 2013
I am having a problem concerning a static const member variable I want to use to set a certain property of my class during development time. The question actually concerns proper implementation as I do have a solution that "works" at least. The variable should denote the size of a member array which I don't want to allocate on the heap due to serious performance issues. So here is my code:
//MyClass.h
class MyClass{
public:
static const int MyArraySize = 256;
private:
int MyArray[MyArraySize];
};
This works but it's not nice for two reasons:
1) It doesn't separate interface from implementation. I would prefer to define the variable in the corresponding .cpp file but it doesn't work:
//MyClass.h
class MyClass{
public:
static const int MyArraySize;
[Code] .....
If I delete the line int MyArray[MyArraySize]; the above code works but when I use it to define the size of the array I get a "constant expression expected" error for the line int MyArray[MyArraySize]; which makes sense as the compiler does not know the value of MyArraySize when he reaches int MyArray[MyArraySize]; and therefore can not allocate the memory. Of course I can move MyArray to the heap like that:
//MyClass.h
class MyClass{
public:
static const int MyArraySize;
static const int MyValue;
[Code] .....
But as I mentioned before this causes a remarkable loss of performance.
Something like the following does not work:
//MyClass.h
class MyClass{
public:
static const int MyArraySize = (int) pow(2, 8);
private:
int MyArray[MyArraySize];
};
This gives a "constant expression expected" error for the line static const int MyArraySize = (int) pow(2, 8);
Interestingly the following code works:
//MyClass.h
class MyClass{
public:
static const int MyValue;
};
//MyClass.cpp
#include "MyClass.h"
const int MyClass::MyValue = (int) pow(2, 8);
So if I use pow outside of the class definition I get no errors. Is there any solution to those problems? So what I want is:
1) Don't allocate the array on the heap
2) Separate interface from implementation
3) Being able to use functions like pow to define MyArraySize
4) Not use global variables
View 19 Replies
View Related
Oct 5, 2014
I've go this code that I need to use a Named Constant for the price of a t-shirt.
I've already done the code from a previous lab for college and I'm not sure how to proceed.
Here is the code:
int main()
{
int tno, price=12,cost;
float discount;
[Code].....
not asking to have this done for me just a hint at where to put the Named Constant
View 2 Replies
View Related
Oct 17, 2014
I've been given specific instructions to create an array inside a Class Matrix using a constant n. This is my class but I am getting errors. I thought that maybe I had to initialize the const and the array using the constructor function Matrix() instead of directly in the class, but I didn't have any luck with that either.
class Matrix
{
public:
Matrix();
private:
const int n=3;
int e[n][n];
};
View 4 Replies
View Related
Jun 17, 2014
How can you convert int type to const char*
View 2 Replies
View Related