C++ :: How To Check Which Variable Has No Value
Nov 12, 2013
I am writing a console program for a class. I have satisfied the assignment, but I want to clear up what is mostly a cosmetic problem. The program prints a form to the console and places the cursor at a location on the form where the user inputs data. The problem occurs when the user presses the enter key without entering data. The cursor goes to the beginning of the next line. If the user enters data after this, the program functions correctly. I want to know how I can reposition the cursor if the user enters no data.
This is the code that reads one of the values:
Code:
void getHousing(HANDLE screen, MonthlyBudget &inputBudget) {
placeCursor(screen, HOUSING_ROW, ACTUAL_COL);
cin >> inputBudget.housing;
while (!validateEntry(screen, inputBudget.housing)) {
placeCursor(screen, HOUSING_ROW, ACTUAL_COL);
cout << SEVEN_SPACES << endl;
placeCursor(screen, HOUSING_ROW, ACTUAL_COL);
cin >> inputBudget.housing;
}
}
validateEntry checks that the entered value is >= 0 SEVEN_SPACES is a string of seven spaces to cover up the previous entry.
View 3 Replies
ADVERTISEMENT
Jun 4, 2014
Ii made one string variable and called it email_confirm. I used cin to get the users email but i don't know to check if the email is real.(without being connected to the internet).
For example,in PHP you can use ereg() or eregi() to see if the email is real, I mean you can see how many characters the email_confirm is made off or how many characters is used after the @ or . is used. is there any functions for this?
View 5 Replies
View Related
May 6, 2013
int main (int argc, char **argv) {
//int i,m;
double v1[kmax],v2[kmax],Ng,Lk[kmax],Hmax,Lmk[N],FD;
//double Lk1 = 0.0;
double x1[N];
loadImage("../sierpinski10.pgm");
printf("Bnorm[40][2] = %f",Bnorm[40][2]); //test
[code]...
Note:I am receiving error "Run-Time Check Failure #2 - Stack around the variable 'Lmk' was corrupted."
View 3 Replies
View Related
Nov 10, 2014
Under visual studio, this is a typical run time error,
Code:
void func(int x){
x = 3;
}
int main() {
int x;
func(x);
}
When x is passed to the function func, it is not initialized. But my question is that why it should be an error? On the other hand, if I change the definition of func a little bit like this,
Code:
void func(int& x) {
*x = 3;
}
int main() {
int x;
func(&x);
}
Now in main, x is still not initialized, but this time there isn't a run time error like "the variable is being used without being initialized. Why?
View 5 Replies
View Related
May 10, 2013
I 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?
View 2 Replies
View Related
May 1, 2013
I am having trouble compiling my interface. I am trying to store a reference variable as a member variable of the interface object. Compiler says that the variable has not be initiated correctly.
LCD inherits from VisualInterface which is expecting a DisplayDriver object to be passed in (DisplayDriver is another interface, but thats not important).
I pass the displayDriver object in when LCD is instantiated in maininterfaces.zip
I was pasing it before as a pointer but was told that this could cause me problems with memory leaks and a reference was better, but now I cant seem to get it to compile.
View 11 Replies
View Related
Mar 2, 2013
This problem is best explained by looking at its output below. I stored 15 different values but when retrieved some are wrong.
#include <iostream>
using namespace std;
class A {
public:
int ** pointer;
[Code] ....
I need to store and retrieve the 15 different values correctly. How to solve this? The main thing is, I need to store the pointer in class A objects.
View 6 Replies
View Related
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
Oct 25, 2014
I need to transform a local variable into a global variable so I can use it in one of my functions. I thought about passing the value as a parameter to this function but I can do this since the function is called inside the while loop and this variable counts how many times the while loop does (so the final value is outside the loop). Example to visualize better:
Code:
while(condition) {
function(parameter1, parameter2);
count = count + 1;
}
printf("%d
", count);
So, I need to transform the final value of "count" into a global variable. Can I do this?
View 5 Replies
View Related
Oct 15, 2013
I want to store few different functions to a variable for different structs/classes and then call it later using that variable, is it possible? something like
struct item {
int ID;
int special; // for function
};
item Key;
Key.special = UseKey(KEY_KING);
// now when I want to call function "UseKey(KEY_KING)" I want to use "Key.special", like this
if(iCurrRoom == ROOM_KING)
Key.special;
else if(iCurrRoom == ROOM_DRAGON)
Fireball.special;
View 5 Replies
View Related
Feb 3, 2014
I've been experimenting with pointers and am getting the below error.
'error: cannot convert 'int**' to 'int*' in assignment'
I thought it was ok to assign a variable address to another variable. Line 18 is where I get the error.
I am trying to show the progression of memory as I increment it as I have done on line 17 and again, I don't know why I don't see a progression through memory locations when output to the console on line 20.
Here's the code:
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main() {
int *tt = new int;
int *p = new int;
[Code] .....
View 6 Replies
View Related
Jun 16, 2013
Any way to create a variable using a variable in the name? So E.g. if you wanted to create an int named nr(x), and x was 1, you would get an int variable named nr1? How would you do this?
View 10 Replies
View Related
Aug 10, 2014
How to change an enum type variable to a string type variable?
View 2 Replies
View Related
Aug 5, 2013
Here is the code,
Code:
class A {
};
A& CreateObject() {
static A a;
return a;
} static A aa;
int main() {
return 0;
}
So is there any difference between a defined in CreateObject and aa?
View 6 Replies
View Related
Sep 13, 2013
I'm using SDL to try to create a Run and Shoot game. But I do not know how to check if a key is down while the user is HOLDING it.
I do know how to check if a key was pressed.
I have tried with the "event.key.keysym.sym" and "Uint8 *keystate = GetKeyState(NULL)" both worked to check if a key was down but I thought that the GetKeyState(); Function would even check when a key where HELD down
I want my player to move while holding down left or right arrow. So I did something like:
Code:
Uint8 *keystate = GetKeyState(NULL);
if (keystate[SDLK_RIGHT]) {
apply_surface(x++, y, player, screen);
}
How to check if a key is held down?
View 2 Replies
View Related
Mar 16, 2014
I'm currently trying to write a while loop that checks if the text file has read all the contents inside. I've tried using
while(!in.eof())
but as usual it executes my loop an extra iteration, printing my last output twice. I am reading my data in from a method inside a class, so I cannot use getline as my while test to check if the file has read input or not. Is there any way to force my loop to check if the end of file has been read before the eof() test is executed?
View 9 Replies
View Related
Apr 14, 2014
write a program to check the number is even or odd using (For loop) in c++ ?
View 10 Replies
View Related
Sep 22, 2013
I'm getting a stack overflow error because for large numbers, this code I'm working on allocates too much on the stack.
Is there a way of tracking stack allocations specifically?
Will multithreading solve my problem if each thread is doing the static allocations?
Would I really have to use malloc or new every time I wanted to use memory just to make my code scale to huge numbers?
View 11 Replies
View Related
Mar 6, 2015
I want to check whether a certain character is in a string or not but my code is not working
Code:
#include<stdio.h>
#include<string.h>
int main()
{
[Code].....
View 7 Replies
View Related
Sep 24, 2013
I've tried retyping the code several times and didn't work for some reason the If wont accept both Q and q it just accepts Q.
Code:
#include <stdio.h>
int main(void)
{
printf("A menu will show up and you choose the number for the selection you want.
[Code].....
View 9 Replies
View Related
Sep 24, 2013
1. Input an dimension and elements of an array from the keyboard. Count the odd elements of an array and that number join to variable K, then sort the last K elements in decreasing order.
Code:
#include <stdio.h>
main ()
{
int A[100], i, n, j, K = 0;
printf ("Type in the dimension of an array:
");
scanf ("%d", &n);
[Code]....
View 7 Replies
View Related
Apr 27, 2013
Code:
char value;
printf_s("enter:");
if (scanf_s("%c", &value) != 1)
{
printf_s("oppppssss
");
}
else
{
printf_s("ok");
}
I wanted to check whether the input is a character or not, if a character is given then the output suppose to be "ok", but the output is always "oppppssss", where is the problem here?
View 1 Replies
View Related
Oct 26, 2013
Any chances for a c program to check for browser inputs?
View 1 Replies
View Related
Sep 18, 2014
I was wondering if it is possible to check if two addresses, so pointers are equal.I was saving the address of an array, and later wanted to identify it by the address, so if my area has the address: int *my_array; // is equal to: 0x1e9aa3a2c ...Later when I go through a list of pointers like:
list=
0x1e9c7e060
0x1e9ba6640
0x1e9aa3a2c <== my address
0x1e9aa3a2c
I want the third one to be equal to my list, but with == it didn't work for me.
View 3 Replies
View Related
Mar 6, 2015
I'm supposed to make a program that can tell which date, out of any number of dates entered by the user, is the earliest date. However, this is based off another program that I did not do in the last chapter. Since it's a pretty simple program to use as the base for the more "generalized" one, I decided to make the more basic one that can only take two dates, first. If it was just one integer, I could just use date1 for the first date and date2 for the second date, but each date uses 6-8 separate numbers.
how do I tell it something like, "if(date1 < date2)", with date1 and date2 including their month, day, and year. I could do a separate integer name/tag (I forgot what they're called) for each number, but that sounds like doing a lot more adding and subtracting, and like it could easily get messy.Should I, or can I, add all the numbers under the "date" together to see which date has the "lowest number" or "earliest date", or should I somehow handle each number, that is month, day, and year, separately?Here's the code so far:
Code:
include <stdio.h>int main(void)
{
int date1, date2;
printf("Enter first date (mm/dd/yy):
");
scanf("%d/%d/%d", &date1);
printf("Enter second date (mm/dd/yy):
}
[code]....
View 10 Replies
View Related
Mar 20, 2013
Description: This program asks the user to enter a Fahrenheit temperature and then converts it into Celsius and Kelvin temperature.
Code :
#include <stdio.h>
void Temperatures(double temp_F);
int main(void) {
double temp;
[Code]...
View 6 Replies
View Related