Solved

integer strangeness

Posted on 2002-04-29
19
225 Views
Last Modified: 2010-04-02
As soon as the following code executes all cout statments with an integer value of over 99 outputs like this 1,00.  I am wondering why this is?
[code]
#include <iostream.h>
#include <conio.h>
#include <string.h>

const string SKILL_LIST ="Bladecraft,Knife";
int main()
{
 string req[2];
 string temp;
 for (int a=0;a<2;a++)
   req[a]="\0";
 int x=0;
 int pager=0;
 int y=0;
 int NUMBER=100;
 clrscr();
 cout << NUMBER <<endl;

 do
 {
   while((SKILL_LIST[x+pager]!=',')&&
         (SKILL_LIST[x+pager]!='\0'))
   {
     temp[x]=SKILL_LIST[x+pager];
     x++;
     cout << NUMBER <<' ';
   }
   cout << NUMBER <<'\n';
   if (SKILL_LIST[x+pager]==',')
     {
       cout << NUMBER <<' ';
       x++;
       pager=x;
       x=0;
       while (x<pager)
       {
         cout << NUMBER <<' ';
         req[y]=req[y]+temp[x];
         x++;
       }
       cout <<'\n';
       req[y]=req[y]+'\0';
       temp=req[y];
       req[y]=temp;
       x=0;
       y++;
       temp[0]='\0';
     }
   else
   {
     cout << NUMBER <<' ';
     int a =0;
     int trash=pager+x;
     while (trash>pager)
     {
       cout << NUMBER <<' ';
       req[y]=req[y]+temp[a];
       pager++;
       a++;
     }
     cout <<'\n';
     req[y]=req[y]+"\0";
   }
 }while (SKILL_LIST[x+pager]!='\0');
 cout << NUMBER;
 getchar();
 return 0;
}//end main

[/code]
0
Comment
Question by:lostminds
  • 7
  • 6
  • 2
  • +3
19 Comments
 
LVL 6

Expert Comment

by:snoegler
ID: 6977263
I see only cout << 100 in the code???
Where are you printing integers / where are you printing anything other than '100', '\n'?
0
 

Author Comment

by:lostminds
ID: 6977746
I don't get what the problem is it is an integer. but to make yah happy I'll change all the 100's to an integer variable which equals 100
0
 
LVL 6

Accepted Solution

by:
snoegler earned 150 total points
ID: 6977766
1. You're using the old, non-standard IOstreams. Change
#include <iostream.h>
to
#include <iostream>

and

#include <string.h>
to
#include <string>

2. Secondly, the line
    temp[x]=SKILL_LIST[x+pager];
   will ASSERT() or give an access violation, because temp is empty to that time. Same for req, if you add another SKILL.

3. If i remove the whole code and replace it by
  cout << "100" << "\n",
   it outputs '100' as expected. Maybe the strange problem is caused by corrupted heap (see 2.)
0
 

Author Comment

by:lostminds
ID: 6978408
Let me try to explain what this does so you can understand (and me too :) )  All of that is in a function, along with a few other lines.  req[0] & req[1] is used to calculate two different numbers which are compared and the greatest of the two numbers is put into a variable that is passed back to the calling function.  The function works perfectly for what i need.  the problem comes when I output other numbers (in variables having nothing to do with this function) which are over 3 digits long (ie 100). I have changed this function to work without the error using strtok() but I'm still wondering why it occurs is it a corrupted heap as snoegler says? Can it be if the code works fine? (pardon my ignorance)
0
 
LVL 6

Expert Comment

by:snoegler
ID: 6978479
Hmm. I can't imagine why in your case cout'ing an integer inserts a ',' while it works correctly in millions of lines of existing code.

The code is pretty obfuscated; it is hard to understand what you want to accomplish. Anyway:

1. The way you want to use strings, i'd rather use a vector.

2. Either way, make sure that 'temp' and 'req' have enough memory reserved (i.e. temp.resize(100); ). In general, can you estimate how large the required space needs to be? (pager increases sometimes, x increases, a increases, all of those are sometimes added together and used as an index)

3. It would help understanding your code if you'd use more descriptive variable names.
0
 

Author Comment

by:lostminds
ID: 6978520
yeah I know twas horrible names :) basically it takes a string from a list ie "Bladecraft,Knife" it then seperates the two words recording "Bladecraft" into req[0] and "Knife" into req[1].  It then passes req[0] to a function which returns an integer value. then it passes req[1] to the same function and returns another integer value. then it compares to two integers and returns the largest of the two.  It worked except for that strange side affect with the integers. btw if I output an integer to the text file it also prints out as 1,00.
0
 
LVL 6

Expert Comment

by:snoegler
ID: 6978636
Maybe you could do the splitting like this (top off my head, may contain typos):

size_t prevpos = 0, curpos = 0;
bool   fContinue = true;
while( fContinue )
  {
  string curpart;
  commapos = SKILL_LIST.find(',',prevpos);
  if(commapos==string::npos)
    {
    fContinue = false;
    curpart = SKILL_LIST.substr(prevpos);
    }
  else
    {
    curpart = SKILL_LIST.substr(prevpos,commapos-prevpos);
    prevpos = commapos+1;
    }

   /* code goes here, curpart contains the substring */
  }


BTW: What output do you get from this code:

#include <iostream>

using namespace std;
int main(int, char*[])
{
  cout << 100 << endl;
}
0
 
LVL 22

Expert Comment

by:ambience
ID: 6979510
>> btw if I output an integer to the text file it also prints out as 1,00.

can you please show us the integer that you are talking about ?, the only integer that i can see being output is  NUMBER and i dont see it get any other value than 100.

Maybe you should try post the exact code with the function that you are talking about.
0
 
LVL 3

Expert Comment

by:MDarling
ID: 6981320
ambience  - the issue is not the value of the integer - it is the way it is displayed.

The problem is if he uses the following...

cout << 100 << endl;

he gets an output of

1,00

instead of

100


Regards,
Mike.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:snoegler
ID: 6981573
Does he really? I'd like to see the output of a simple

#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
  cout << 100 << endl;
}
0
 
LVL 3

Expert Comment

by:MDarling
ID: 6981604
yeah me too.
0
 

Author Comment

by:lostminds
ID: 6981616
I'll post it very simply this is my cout statement that i use in my main program.

cout << "This is the best value : ";
skill_value=Get_Value(Skill_text);
cout << skill_value <<".";

the above (which is inside the Get_Value function) has at the end.

value_1=Determine_value(req[0]);
value_2=Determine_value(req[1]);

(value_1>value_2)?(to_return=value_1):(to_return=value_2);
return(to_return);

That is all it does, Determine_value is an integer returning function.  Yes I changed the function around to run by itself to make show what the problem was.  It does not matter what I output all "integers" not characters is changed to have a comma in them if they are 3 digits in length. Yes I do need to output them as integers. For I use the values in formulas later on.
0
 

Author Comment

by:lostminds
ID: 6981617
I have changed the function to use strtok() instead, and this doesn't cause this error.  The question is why does this piece of code cause that error?
0
 
LVL 6

Expert Comment

by:snoegler
ID: 6981670
Because you simply corrupt the heap. I don't get why such an unusual behavior should result, but this is definitely the cause. If i compile your code & run it on my machine, i get instantly an access violation.

CN: Use not temp[5], but temp.at(5) - this ASSERT's if the index is out of bounds, so you'll know where you overwrite unallocated memory.
0
 

Author Comment

by:lostminds
ID: 6981689
do you mean change the declare from
string req[2];
to
string req.at(2);

? (pardon my ignorance) :)
0
 
LVL 6

Expert Comment

by:snoegler
ID: 6981710
No, more like this:

while (trash>pager)
    {
      cout << NUMBER <<' ';
      req.at(y)=req.at(y)+temp.at(a);
      pager++;
      a++;
    }

instead of

while (trash>pager)
    {
      cout << NUMBER <<' ';
      req[y]=req[y]+temp[a];
      pager++;
      a++;
    }


0
 
LVL 22

Expert Comment

by:ambience
ID: 6983097
ok that is definitely memory corruption, try my weirdo sample

  while((SKILL_LIST[x+pager]!=',')&&
        (SKILL_LIST[x+pager]!='\0'))
  {
    temp += SKILL_LIST[x+pager];
    x++;
    cout << NUMBER <<' ';
  }
  cout << NUMBER <<'\n';

  if (SKILL_LIST[x+pager]==',')
    {
      cout << NUMBER <<' ';
      x++;
      pager=x;
      x=0;
      while (x < pager)
      {
        cout << NUMBER <<' ';
        req[y] += temp[x];
        x++;
      }
      cout <<'\n';
      req[y] += '\0';

      temp=req[y];
      req[y]=temp;
      x=0;
      y++;
      temp = "";
    }
  else
  {
    cout << NUMBER <<' ';
    int a =0;
    int trash=pager+x;
    while (trash>pager)
    {
      cout << NUMBER <<' ';
      req[y] += temp[a];
      pager++;
      a++;
    }
    cout <<'\n';
    req[y] += "\0";
0
 
LVL 11

Expert Comment

by:griessh
ID: 7122202
Dear lostminds

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "snoegler"

comment(s) as an answer.
     "refund the points and delete this question"
since nobody had a satisfying answer for you.
since you never gave more feedback.
PAQ at zero points.

If you think your question was not answered at all, you can explain here why you want to do this and post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/


PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7138118
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now