#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <math.h>
#include <windows.h>

using namespace std;

int main()
{
    int l,  pos, pos1;
    unsigned long int currnum;
    string str0, str1;
    stringstream out;
    string::iterator x1, x2;
    float koren;
    bool find = 0;
    ifstream fin("koren.in");
    ofstream fout("koren.out");
    if(fin.is_open())
    {
                 fin>>l;
                 fin>>str0;
                 fin.close();
                 currnum=159;
                 while(find==0)
                 {
                                  koren=sqrt(currnum);
                                  out<<koren;
                                  str1=out.str();
                                  pos=str1.find(".");
                                  if(pos!=string::npos)
                                  {
                                  str1.erase(pos+1+l);
                                  if(str1.find(str0, pos)!=string::npos)
                                  find=1;
                                  else
                                  currnum++;
                                  }
                                  else
                                  {
                                  currnum++;
                                  }
                                  out.str(string());
                 }
                 fout<<currnum;
                 fout.close();
    }
    else
    {
        cout<<"Fajlot nemoze da se otvori";
    }
    return 0;
}

It crashes when currnum gets to 156

What is this program supposed to do?

get the lowest number that has it's square root decimals starting with str0. It also uses an input file that has the number of decimals it needs to compare in the first line and str0 in the second line.

l is a terrible name for a variable because it is very difficult to tell if it is an el or a one. Ditto for 1. Use something more desciptive, maybe something like numDigits.

Don't use variable names that are the same as a function name, like find. Again too easy to get confused, if not you, then someone reviewing your code trying to determine which find you really want to use.

Use true and false, rather than 0 or 1 when working with boolean variables to make it easier to tell what you are doing.

Use spaces instead of tabs to indent. It makes it easier to keep the code on a single line when there are multiple layers of nesting, and appropriate use of indentation is critical to trying to determine appropriate nesting of loops, which statements belong to which loop, etc. Repost your code with this change.

If I understand the requirements correctly say koren is 11.35679 and str0 is 35 you want to see if the digits of the decimal portion of koren starts with the same digits as in str0. In this case you can tell visually that it does, but how would you do it on the computer? Here's pseudocode for how I'd try it.

//convert sqrt to type double
//look for decimal point

//if decimal point found
   //look for str0 starting at next position after pos
       //if str0 is in str1 somewhere
           //if first occurrence of str0 is right after decimal
               //change flag variable to true

its not the problem how its done.. its working perfectly for square roots of numbers smaller than 156.. the thing is... it crashes when it reaches 156. Here's a revised code:

#include <fstream>
#include <string>
#include <sstream>
#include <math.h>

using namespace std;

int main()
{
    int length,  pos, pos1;
    unsigned long int currnum;
    string str0, str1;
    stringstream out;
    string::iterator x1, x2;
    float koren;
    bool find = 0;
    ifstream fin("koren.in");
    ofstream fout("koren.out");
    if(fin.is_open())
    {
                 fin>>length;
                 fin>>str0;
                 fin.close();
                 currnum=0;
                 while(find==0)
                 {
                                  koren=sqrt(currnum);
                                  out<<koren;
                                  str1=out.str();
                                  pos=str1.find(".");
                                  if(pos!=string::npos)
                                  {
                                  str1.erase(pos+1+length);
                                  pos1=str1.find(str0, pos);
                                  if(pos1!=string::npos)
                                  find=1;
                                  else
                                  currnum++;
                                  }
                                  else
                                  {
                                  currnum++;
                                  }
                                  out.str(string());
                 }
                 fout<<currnum;
                 fout.close();
    }
    return 0;
}

I take it you mean it crashes when currNum is 156.

If so, hard code the program so currnum is 156 and place lots of output statements and pause the program after each action to determine the value of each variable at each step.

koren = sqrt(currnum);
cout << "koren = " << koren << endl;
cin.get();

out << koren;
str1=out.str();
cout << "str1 = " << str1 << endl;
cin.get();

pos=str1.find(".");
if(pos != string::npos)
{
    cout << "pos = " << pos << endl;
    cin.get();
   //etc

That should give you an idea of where the code crashes, and maybe why. If that sounds like to much hassle, then use your favorite debugger to help you out. I don't have a compiler handy to do it at this time.

I debugged it and it crashes exactly at
pos1=str1.find(str0, pos);
Any ideas?

no no it crashes after it passes str1.erase(pos+1+length);

...This confuses me...
I outputted the str1 after the square root of 156 got passed to it and it was saying 12.49. i guess this was the reason for the crashes, but why is it saying 12.49 instead of more decimals. calculator says 12,48999599679679641169378624188 and so does the debugger watching the value koren. wtf is wrong!

My version of your code cruises right through 156 if I let currnum range from 2 to 200 and I set str0 to 4899 (I get 132 if I use a str0 of 489). I do get the truncation to 12.49 as the sqrt of 156. I haven't been able to figure out how to tell the compiler to keep more significant digits than the default number.

What value(s) of str0 are you looking for when you crash at 156 so I can try it?

try length 3, str0 529

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.