Why is can't num scan the intended value in c?












-1















I have to scan this file which partly contains



SNOL
INTO num IS 8
INTO res IS 9


and the output of the code below is



Program starts...
Set value of num to 0
Set value of res to 8
input msg


which is wrong because num should be 8 and res should be 9



why is it num scanning 0 instead of 8?



and why doesn't the code work anymore if I assign number to num and number to res?



num = number;

//Tokenizer functions//
bool isLowerCase(const char *object)
{
int i;
int len = strlen(object);
for(i = 0; i < len; i++) {
if(object[i] >= 'a' && object[i] <= 'z') {
return true;
}
}
return false;
}
//function to check if character is Float.
bool objectFloat(const char* object) {
//check if 1st character is a digit, if not then return false,
otherwise
return true.
if(!isdigit(object[0]))
return false;

// Check if the 2nd character to the last are digits or periods.
// If not, return false otherwisereturn true
int periods = 0; //initialize how many periods in the object to zero
int i;
//if character is a period then increment periods.
for(i = 1; i < strlen(object); i++) {
if(object[i] == '.') {
periods++;
}
//return false if character is not a digit
else if(!isdigit(object[i])) {
return false;
}
}
// return true if there is only one period.
return periods == 1;
}

//function to check if character is a keyobject.
bool isKeyobject(const char* object) {
char keyobjects[11][11] = { "SNOL", "LONS", "INTO", "IS", "MULT", "BEG",
"PRINT", "ADD", "SUB", "DIV", "MOD" };

int i;
for(i = 0; i < 11; i++) {
// Check if object is equal to keyobjects at index i
// If yes, return true
if(isLowerCase(object))
return false;
if(strcmp(object, keyobjects[i]) == 0) {
return true;
}
}
//object is not equal to any of the keyobjects so return false
return false;
}

//Function to check if every character is an integer
// If not, return false otherwise return true
bool objectInt(const char* object) {

int i;
for(i = 0; i < strlen(object); i++) {
if(!isdigit(object[i])) return false;
}

return true;
}

bool objectIsVariable(const char* object) {

// Check if alphanumeric character & lower case
// If not, return false
int i;

for(i = 0; i < strlen(object); i++) {

if(!isalnum(object[i]) && !isLowerCase(object)) return false;
}
return true;
}

int main() {

FILE *s_path = fopen("test.snol", "r");

int number = 0;
int num, res;
if(isKeyobject(object) && strcmp(object, IsitSNOL) == 0) {
printf("Program starts...n");

}
else if(isKeyobject(object) && strcmp(object, IsitINTO) == 0) {
printf("Set value of ");
}

if(objectInt(object)) {
number = atoi(object);
}
else if(objectFloat(object)) {
number = atof(object);
}
if(objectIsVariable(object) && strcmp(object, IsitNum) == 0) {
//if float

printf("num to %dn", number);

num == number;
}
else if(objectIsVariable(object) && strcmp(object, IsitRes) == 0) {
//if float
printf("res to %dn", number);
res == number;
}
else if(isKeyobject(object) && strcmp(object, IsitBEG) == 0) {
printf("input msgn");

scanf("%s", msg);

fscanf(s_path, " %s", &object);
printf("INPUT(%s): %sn", object, msg);
}
}
} // END MAIN -----------------------------------//









share|improve this question




















  • 1





    Might I ask where the rest of your code is? Or better a Minimal, Complete, and Verifiable example?

    – Swordfish
    Nov 24 '18 at 8:00











  • Debug this by inserting a printf of object in every program path. Especially you want a print when you assign to num. I guess you'll see that the code doesn't execute in the order you think

    – 4386427
    Nov 24 '18 at 8:03













  • I was still sitting there bewildered at what isKeyobject or objectFloat or objectIsVariable might possibly be... We can't help you unless you help us help you by posting an actual copy of your code in a compilable form with the minimum code necessary to allow us to compile an reproduce the problem. That is a MCVE.

    – David C. Rankin
    Nov 24 '18 at 8:03











  • I have put printfs of object and it still does not work

    – Robyyyn
    Nov 24 '18 at 8:12
















-1















I have to scan this file which partly contains



SNOL
INTO num IS 8
INTO res IS 9


and the output of the code below is



Program starts...
Set value of num to 0
Set value of res to 8
input msg


which is wrong because num should be 8 and res should be 9



why is it num scanning 0 instead of 8?



and why doesn't the code work anymore if I assign number to num and number to res?



num = number;

//Tokenizer functions//
bool isLowerCase(const char *object)
{
int i;
int len = strlen(object);
for(i = 0; i < len; i++) {
if(object[i] >= 'a' && object[i] <= 'z') {
return true;
}
}
return false;
}
//function to check if character is Float.
bool objectFloat(const char* object) {
//check if 1st character is a digit, if not then return false,
otherwise
return true.
if(!isdigit(object[0]))
return false;

// Check if the 2nd character to the last are digits or periods.
// If not, return false otherwisereturn true
int periods = 0; //initialize how many periods in the object to zero
int i;
//if character is a period then increment periods.
for(i = 1; i < strlen(object); i++) {
if(object[i] == '.') {
periods++;
}
//return false if character is not a digit
else if(!isdigit(object[i])) {
return false;
}
}
// return true if there is only one period.
return periods == 1;
}

//function to check if character is a keyobject.
bool isKeyobject(const char* object) {
char keyobjects[11][11] = { "SNOL", "LONS", "INTO", "IS", "MULT", "BEG",
"PRINT", "ADD", "SUB", "DIV", "MOD" };

int i;
for(i = 0; i < 11; i++) {
// Check if object is equal to keyobjects at index i
// If yes, return true
if(isLowerCase(object))
return false;
if(strcmp(object, keyobjects[i]) == 0) {
return true;
}
}
//object is not equal to any of the keyobjects so return false
return false;
}

//Function to check if every character is an integer
// If not, return false otherwise return true
bool objectInt(const char* object) {

int i;
for(i = 0; i < strlen(object); i++) {
if(!isdigit(object[i])) return false;
}

return true;
}

bool objectIsVariable(const char* object) {

// Check if alphanumeric character & lower case
// If not, return false
int i;

for(i = 0; i < strlen(object); i++) {

if(!isalnum(object[i]) && !isLowerCase(object)) return false;
}
return true;
}

int main() {

FILE *s_path = fopen("test.snol", "r");

int number = 0;
int num, res;
if(isKeyobject(object) && strcmp(object, IsitSNOL) == 0) {
printf("Program starts...n");

}
else if(isKeyobject(object) && strcmp(object, IsitINTO) == 0) {
printf("Set value of ");
}

if(objectInt(object)) {
number = atoi(object);
}
else if(objectFloat(object)) {
number = atof(object);
}
if(objectIsVariable(object) && strcmp(object, IsitNum) == 0) {
//if float

printf("num to %dn", number);

num == number;
}
else if(objectIsVariable(object) && strcmp(object, IsitRes) == 0) {
//if float
printf("res to %dn", number);
res == number;
}
else if(isKeyobject(object) && strcmp(object, IsitBEG) == 0) {
printf("input msgn");

scanf("%s", msg);

fscanf(s_path, " %s", &object);
printf("INPUT(%s): %sn", object, msg);
}
}
} // END MAIN -----------------------------------//









share|improve this question




















  • 1





    Might I ask where the rest of your code is? Or better a Minimal, Complete, and Verifiable example?

    – Swordfish
    Nov 24 '18 at 8:00











  • Debug this by inserting a printf of object in every program path. Especially you want a print when you assign to num. I guess you'll see that the code doesn't execute in the order you think

    – 4386427
    Nov 24 '18 at 8:03













  • I was still sitting there bewildered at what isKeyobject or objectFloat or objectIsVariable might possibly be... We can't help you unless you help us help you by posting an actual copy of your code in a compilable form with the minimum code necessary to allow us to compile an reproduce the problem. That is a MCVE.

    – David C. Rankin
    Nov 24 '18 at 8:03











  • I have put printfs of object and it still does not work

    – Robyyyn
    Nov 24 '18 at 8:12














-1












-1








-1








I have to scan this file which partly contains



SNOL
INTO num IS 8
INTO res IS 9


and the output of the code below is



Program starts...
Set value of num to 0
Set value of res to 8
input msg


which is wrong because num should be 8 and res should be 9



why is it num scanning 0 instead of 8?



and why doesn't the code work anymore if I assign number to num and number to res?



num = number;

//Tokenizer functions//
bool isLowerCase(const char *object)
{
int i;
int len = strlen(object);
for(i = 0; i < len; i++) {
if(object[i] >= 'a' && object[i] <= 'z') {
return true;
}
}
return false;
}
//function to check if character is Float.
bool objectFloat(const char* object) {
//check if 1st character is a digit, if not then return false,
otherwise
return true.
if(!isdigit(object[0]))
return false;

// Check if the 2nd character to the last are digits or periods.
// If not, return false otherwisereturn true
int periods = 0; //initialize how many periods in the object to zero
int i;
//if character is a period then increment periods.
for(i = 1; i < strlen(object); i++) {
if(object[i] == '.') {
periods++;
}
//return false if character is not a digit
else if(!isdigit(object[i])) {
return false;
}
}
// return true if there is only one period.
return periods == 1;
}

//function to check if character is a keyobject.
bool isKeyobject(const char* object) {
char keyobjects[11][11] = { "SNOL", "LONS", "INTO", "IS", "MULT", "BEG",
"PRINT", "ADD", "SUB", "DIV", "MOD" };

int i;
for(i = 0; i < 11; i++) {
// Check if object is equal to keyobjects at index i
// If yes, return true
if(isLowerCase(object))
return false;
if(strcmp(object, keyobjects[i]) == 0) {
return true;
}
}
//object is not equal to any of the keyobjects so return false
return false;
}

//Function to check if every character is an integer
// If not, return false otherwise return true
bool objectInt(const char* object) {

int i;
for(i = 0; i < strlen(object); i++) {
if(!isdigit(object[i])) return false;
}

return true;
}

bool objectIsVariable(const char* object) {

// Check if alphanumeric character & lower case
// If not, return false
int i;

for(i = 0; i < strlen(object); i++) {

if(!isalnum(object[i]) && !isLowerCase(object)) return false;
}
return true;
}

int main() {

FILE *s_path = fopen("test.snol", "r");

int number = 0;
int num, res;
if(isKeyobject(object) && strcmp(object, IsitSNOL) == 0) {
printf("Program starts...n");

}
else if(isKeyobject(object) && strcmp(object, IsitINTO) == 0) {
printf("Set value of ");
}

if(objectInt(object)) {
number = atoi(object);
}
else if(objectFloat(object)) {
number = atof(object);
}
if(objectIsVariable(object) && strcmp(object, IsitNum) == 0) {
//if float

printf("num to %dn", number);

num == number;
}
else if(objectIsVariable(object) && strcmp(object, IsitRes) == 0) {
//if float
printf("res to %dn", number);
res == number;
}
else if(isKeyobject(object) && strcmp(object, IsitBEG) == 0) {
printf("input msgn");

scanf("%s", msg);

fscanf(s_path, " %s", &object);
printf("INPUT(%s): %sn", object, msg);
}
}
} // END MAIN -----------------------------------//









share|improve this question
















I have to scan this file which partly contains



SNOL
INTO num IS 8
INTO res IS 9


and the output of the code below is



Program starts...
Set value of num to 0
Set value of res to 8
input msg


which is wrong because num should be 8 and res should be 9



why is it num scanning 0 instead of 8?



and why doesn't the code work anymore if I assign number to num and number to res?



num = number;

//Tokenizer functions//
bool isLowerCase(const char *object)
{
int i;
int len = strlen(object);
for(i = 0; i < len; i++) {
if(object[i] >= 'a' && object[i] <= 'z') {
return true;
}
}
return false;
}
//function to check if character is Float.
bool objectFloat(const char* object) {
//check if 1st character is a digit, if not then return false,
otherwise
return true.
if(!isdigit(object[0]))
return false;

// Check if the 2nd character to the last are digits or periods.
// If not, return false otherwisereturn true
int periods = 0; //initialize how many periods in the object to zero
int i;
//if character is a period then increment periods.
for(i = 1; i < strlen(object); i++) {
if(object[i] == '.') {
periods++;
}
//return false if character is not a digit
else if(!isdigit(object[i])) {
return false;
}
}
// return true if there is only one period.
return periods == 1;
}

//function to check if character is a keyobject.
bool isKeyobject(const char* object) {
char keyobjects[11][11] = { "SNOL", "LONS", "INTO", "IS", "MULT", "BEG",
"PRINT", "ADD", "SUB", "DIV", "MOD" };

int i;
for(i = 0; i < 11; i++) {
// Check if object is equal to keyobjects at index i
// If yes, return true
if(isLowerCase(object))
return false;
if(strcmp(object, keyobjects[i]) == 0) {
return true;
}
}
//object is not equal to any of the keyobjects so return false
return false;
}

//Function to check if every character is an integer
// If not, return false otherwise return true
bool objectInt(const char* object) {

int i;
for(i = 0; i < strlen(object); i++) {
if(!isdigit(object[i])) return false;
}

return true;
}

bool objectIsVariable(const char* object) {

// Check if alphanumeric character & lower case
// If not, return false
int i;

for(i = 0; i < strlen(object); i++) {

if(!isalnum(object[i]) && !isLowerCase(object)) return false;
}
return true;
}

int main() {

FILE *s_path = fopen("test.snol", "r");

int number = 0;
int num, res;
if(isKeyobject(object) && strcmp(object, IsitSNOL) == 0) {
printf("Program starts...n");

}
else if(isKeyobject(object) && strcmp(object, IsitINTO) == 0) {
printf("Set value of ");
}

if(objectInt(object)) {
number = atoi(object);
}
else if(objectFloat(object)) {
number = atof(object);
}
if(objectIsVariable(object) && strcmp(object, IsitNum) == 0) {
//if float

printf("num to %dn", number);

num == number;
}
else if(objectIsVariable(object) && strcmp(object, IsitRes) == 0) {
//if float
printf("res to %dn", number);
res == number;
}
else if(isKeyobject(object) && strcmp(object, IsitBEG) == 0) {
printf("input msgn");

scanf("%s", msg);

fscanf(s_path, " %s", &object);
printf("INPUT(%s): %sn", object, msg);
}
}
} // END MAIN -----------------------------------//






c integer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 11:46









phuclv

14.9k852220




14.9k852220










asked Nov 24 '18 at 7:53









RobyyynRobyyyn

14




14








  • 1





    Might I ask where the rest of your code is? Or better a Minimal, Complete, and Verifiable example?

    – Swordfish
    Nov 24 '18 at 8:00











  • Debug this by inserting a printf of object in every program path. Especially you want a print when you assign to num. I guess you'll see that the code doesn't execute in the order you think

    – 4386427
    Nov 24 '18 at 8:03













  • I was still sitting there bewildered at what isKeyobject or objectFloat or objectIsVariable might possibly be... We can't help you unless you help us help you by posting an actual copy of your code in a compilable form with the minimum code necessary to allow us to compile an reproduce the problem. That is a MCVE.

    – David C. Rankin
    Nov 24 '18 at 8:03











  • I have put printfs of object and it still does not work

    – Robyyyn
    Nov 24 '18 at 8:12














  • 1





    Might I ask where the rest of your code is? Or better a Minimal, Complete, and Verifiable example?

    – Swordfish
    Nov 24 '18 at 8:00











  • Debug this by inserting a printf of object in every program path. Especially you want a print when you assign to num. I guess you'll see that the code doesn't execute in the order you think

    – 4386427
    Nov 24 '18 at 8:03













  • I was still sitting there bewildered at what isKeyobject or objectFloat or objectIsVariable might possibly be... We can't help you unless you help us help you by posting an actual copy of your code in a compilable form with the minimum code necessary to allow us to compile an reproduce the problem. That is a MCVE.

    – David C. Rankin
    Nov 24 '18 at 8:03











  • I have put printfs of object and it still does not work

    – Robyyyn
    Nov 24 '18 at 8:12








1




1





Might I ask where the rest of your code is? Or better a Minimal, Complete, and Verifiable example?

– Swordfish
Nov 24 '18 at 8:00





Might I ask where the rest of your code is? Or better a Minimal, Complete, and Verifiable example?

– Swordfish
Nov 24 '18 at 8:00













Debug this by inserting a printf of object in every program path. Especially you want a print when you assign to num. I guess you'll see that the code doesn't execute in the order you think

– 4386427
Nov 24 '18 at 8:03







Debug this by inserting a printf of object in every program path. Especially you want a print when you assign to num. I guess you'll see that the code doesn't execute in the order you think

– 4386427
Nov 24 '18 at 8:03















I was still sitting there bewildered at what isKeyobject or objectFloat or objectIsVariable might possibly be... We can't help you unless you help us help you by posting an actual copy of your code in a compilable form with the minimum code necessary to allow us to compile an reproduce the problem. That is a MCVE.

– David C. Rankin
Nov 24 '18 at 8:03





I was still sitting there bewildered at what isKeyobject or objectFloat or objectIsVariable might possibly be... We can't help you unless you help us help you by posting an actual copy of your code in a compilable form with the minimum code necessary to allow us to compile an reproduce the problem. That is a MCVE.

– David C. Rankin
Nov 24 '18 at 8:03













I have put printfs of object and it still does not work

– Robyyyn
Nov 24 '18 at 8:12





I have put printfs of object and it still does not work

– Robyyyn
Nov 24 '18 at 8:12












1 Answer
1






active

oldest

votes


















0














The problem seem to be that you read the number after the variable name but you do the print before.



So your sequence is:



Keyobject INTO
objectIsVariable num // Now you print the value
objectInt // Now you read the value


You need to postpone the printing until you have actually read the value.



This is not a very elegant solution, but you can try like:



int flag = 0;

if(objectInt(object)) {
number = atoi(object);
if (flag == 1)
{
num = number;
printf("num to %dn", number);
}
else if (flag == 2)
{
res = number;
printf("res to %dn", number);
}
else
{
printf("Illegal flagn");
}
flag = 0;
}


if(objectIsVariable(object) && strcmp(object, IsitNum) == 0) {
flag = 1;
}
else if(objectIsVariable(object) && strcmp(object, IsitRes) == 0) {
flag = 2;
}





share|improve this answer


























  • It still doesn't work

    – Robyyyn
    Nov 24 '18 at 8:16











  • @Robyyyn what change did you make? I didn't propose any solution. I only described what is going wrong

    – 4386427
    Nov 24 '18 at 8:17













  • else if (isKeyobject(object)&&strcmp(object, IsitINTO)==0){ else if (objectIsVariable(object)&&strcmp(object, IsitNum)==0){ //if float printf("num to %dn", number); num==number; } if(objectInt(object)){ number=atoi(object); } else if(objectFloat(object)){ number=atof(object); }

    – Robyyyn
    Nov 24 '18 at 8:20











  • @Robyyyn See update

    – 4386427
    Nov 24 '18 at 8:25











  • it works now, thank you. why is there another problem about the code not working when num = number; is used?

    – Robyyyn
    Nov 24 '18 at 8:28













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456264%2fwhy-is-cant-num-scan-the-intended-value-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The problem seem to be that you read the number after the variable name but you do the print before.



So your sequence is:



Keyobject INTO
objectIsVariable num // Now you print the value
objectInt // Now you read the value


You need to postpone the printing until you have actually read the value.



This is not a very elegant solution, but you can try like:



int flag = 0;

if(objectInt(object)) {
number = atoi(object);
if (flag == 1)
{
num = number;
printf("num to %dn", number);
}
else if (flag == 2)
{
res = number;
printf("res to %dn", number);
}
else
{
printf("Illegal flagn");
}
flag = 0;
}


if(objectIsVariable(object) && strcmp(object, IsitNum) == 0) {
flag = 1;
}
else if(objectIsVariable(object) && strcmp(object, IsitRes) == 0) {
flag = 2;
}





share|improve this answer


























  • It still doesn't work

    – Robyyyn
    Nov 24 '18 at 8:16











  • @Robyyyn what change did you make? I didn't propose any solution. I only described what is going wrong

    – 4386427
    Nov 24 '18 at 8:17













  • else if (isKeyobject(object)&&strcmp(object, IsitINTO)==0){ else if (objectIsVariable(object)&&strcmp(object, IsitNum)==0){ //if float printf("num to %dn", number); num==number; } if(objectInt(object)){ number=atoi(object); } else if(objectFloat(object)){ number=atof(object); }

    – Robyyyn
    Nov 24 '18 at 8:20











  • @Robyyyn See update

    – 4386427
    Nov 24 '18 at 8:25











  • it works now, thank you. why is there another problem about the code not working when num = number; is used?

    – Robyyyn
    Nov 24 '18 at 8:28


















0














The problem seem to be that you read the number after the variable name but you do the print before.



So your sequence is:



Keyobject INTO
objectIsVariable num // Now you print the value
objectInt // Now you read the value


You need to postpone the printing until you have actually read the value.



This is not a very elegant solution, but you can try like:



int flag = 0;

if(objectInt(object)) {
number = atoi(object);
if (flag == 1)
{
num = number;
printf("num to %dn", number);
}
else if (flag == 2)
{
res = number;
printf("res to %dn", number);
}
else
{
printf("Illegal flagn");
}
flag = 0;
}


if(objectIsVariable(object) && strcmp(object, IsitNum) == 0) {
flag = 1;
}
else if(objectIsVariable(object) && strcmp(object, IsitRes) == 0) {
flag = 2;
}





share|improve this answer


























  • It still doesn't work

    – Robyyyn
    Nov 24 '18 at 8:16











  • @Robyyyn what change did you make? I didn't propose any solution. I only described what is going wrong

    – 4386427
    Nov 24 '18 at 8:17













  • else if (isKeyobject(object)&&strcmp(object, IsitINTO)==0){ else if (objectIsVariable(object)&&strcmp(object, IsitNum)==0){ //if float printf("num to %dn", number); num==number; } if(objectInt(object)){ number=atoi(object); } else if(objectFloat(object)){ number=atof(object); }

    – Robyyyn
    Nov 24 '18 at 8:20











  • @Robyyyn See update

    – 4386427
    Nov 24 '18 at 8:25











  • it works now, thank you. why is there another problem about the code not working when num = number; is used?

    – Robyyyn
    Nov 24 '18 at 8:28
















0












0








0







The problem seem to be that you read the number after the variable name but you do the print before.



So your sequence is:



Keyobject INTO
objectIsVariable num // Now you print the value
objectInt // Now you read the value


You need to postpone the printing until you have actually read the value.



This is not a very elegant solution, but you can try like:



int flag = 0;

if(objectInt(object)) {
number = atoi(object);
if (flag == 1)
{
num = number;
printf("num to %dn", number);
}
else if (flag == 2)
{
res = number;
printf("res to %dn", number);
}
else
{
printf("Illegal flagn");
}
flag = 0;
}


if(objectIsVariable(object) && strcmp(object, IsitNum) == 0) {
flag = 1;
}
else if(objectIsVariable(object) && strcmp(object, IsitRes) == 0) {
flag = 2;
}





share|improve this answer















The problem seem to be that you read the number after the variable name but you do the print before.



So your sequence is:



Keyobject INTO
objectIsVariable num // Now you print the value
objectInt // Now you read the value


You need to postpone the printing until you have actually read the value.



This is not a very elegant solution, but you can try like:



int flag = 0;

if(objectInt(object)) {
number = atoi(object);
if (flag == 1)
{
num = number;
printf("num to %dn", number);
}
else if (flag == 2)
{
res = number;
printf("res to %dn", number);
}
else
{
printf("Illegal flagn");
}
flag = 0;
}


if(objectIsVariable(object) && strcmp(object, IsitNum) == 0) {
flag = 1;
}
else if(objectIsVariable(object) && strcmp(object, IsitRes) == 0) {
flag = 2;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 8:39

























answered Nov 24 '18 at 8:14









43864274386427

21.1k31845




21.1k31845













  • It still doesn't work

    – Robyyyn
    Nov 24 '18 at 8:16











  • @Robyyyn what change did you make? I didn't propose any solution. I only described what is going wrong

    – 4386427
    Nov 24 '18 at 8:17













  • else if (isKeyobject(object)&&strcmp(object, IsitINTO)==0){ else if (objectIsVariable(object)&&strcmp(object, IsitNum)==0){ //if float printf("num to %dn", number); num==number; } if(objectInt(object)){ number=atoi(object); } else if(objectFloat(object)){ number=atof(object); }

    – Robyyyn
    Nov 24 '18 at 8:20











  • @Robyyyn See update

    – 4386427
    Nov 24 '18 at 8:25











  • it works now, thank you. why is there another problem about the code not working when num = number; is used?

    – Robyyyn
    Nov 24 '18 at 8:28





















  • It still doesn't work

    – Robyyyn
    Nov 24 '18 at 8:16











  • @Robyyyn what change did you make? I didn't propose any solution. I only described what is going wrong

    – 4386427
    Nov 24 '18 at 8:17













  • else if (isKeyobject(object)&&strcmp(object, IsitINTO)==0){ else if (objectIsVariable(object)&&strcmp(object, IsitNum)==0){ //if float printf("num to %dn", number); num==number; } if(objectInt(object)){ number=atoi(object); } else if(objectFloat(object)){ number=atof(object); }

    – Robyyyn
    Nov 24 '18 at 8:20











  • @Robyyyn See update

    – 4386427
    Nov 24 '18 at 8:25











  • it works now, thank you. why is there another problem about the code not working when num = number; is used?

    – Robyyyn
    Nov 24 '18 at 8:28



















It still doesn't work

– Robyyyn
Nov 24 '18 at 8:16





It still doesn't work

– Robyyyn
Nov 24 '18 at 8:16













@Robyyyn what change did you make? I didn't propose any solution. I only described what is going wrong

– 4386427
Nov 24 '18 at 8:17







@Robyyyn what change did you make? I didn't propose any solution. I only described what is going wrong

– 4386427
Nov 24 '18 at 8:17















else if (isKeyobject(object)&&strcmp(object, IsitINTO)==0){ else if (objectIsVariable(object)&&strcmp(object, IsitNum)==0){ //if float printf("num to %dn", number); num==number; } if(objectInt(object)){ number=atoi(object); } else if(objectFloat(object)){ number=atof(object); }

– Robyyyn
Nov 24 '18 at 8:20





else if (isKeyobject(object)&&strcmp(object, IsitINTO)==0){ else if (objectIsVariable(object)&&strcmp(object, IsitNum)==0){ //if float printf("num to %dn", number); num==number; } if(objectInt(object)){ number=atoi(object); } else if(objectFloat(object)){ number=atof(object); }

– Robyyyn
Nov 24 '18 at 8:20













@Robyyyn See update

– 4386427
Nov 24 '18 at 8:25





@Robyyyn See update

– 4386427
Nov 24 '18 at 8:25













it works now, thank you. why is there another problem about the code not working when num = number; is used?

– Robyyyn
Nov 24 '18 at 8:28







it works now, thank you. why is there another problem about the code not working when num = number; is used?

– Robyyyn
Nov 24 '18 at 8:28






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456264%2fwhy-is-cant-num-scan-the-intended-value-in-c%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python