Link to home
Start Free TrialLog in
Avatar of hthukral
hthukral

asked on

Simple While Loop Stuff

Hello Everyone

I have an algorithm from where I'm getting the results...something like this...

while(sp != NULL)
      {
            // Conversion from std::string to QString
            QString t(destination.c_str());

            if (sp == G.map[(G.find_node(G.map, source))])
            {
                  ip1 = sp->name.c_str();
                                    
            }
            else
            {
            
                  ip2 = sp->name.c_str();
                  
            }

            sp = sp->next;

      }      // end of while

Ip1 gives me the first starting point and then , if you go further ip2 gives me the result ......this is based on Dijkstra Algorithm.....

suppose you say to want to go from A - D....so it comes like

first A -- result comes in ip1, then A ---> B---> result comes in ip2
then B--->D---> result comes in ip2

Ip2 value is changing all the time.....how can I grab all the values...

Its so stupid question for here....but can't get my brain to work....
Avatar of beryl666
beryl666
Flag of United States of America image

you need to use linked list to implement this: ( i just roughly show you the concept here, it is not the exact code)

 struct ListNode
   {
    string item;
   ListNode* next;
   };

ListNode *head, *tail, *cur, *prev;  
head=tail=cur=prev=NULL;
head->item=A;
cur=cur->next;
head->next=cur;\\connect to B
cur->item=B;
cur=cur->next;\\connect to D
cur->item=D;
cur->next=NULL;
tail=cur;


ok now?
after that you can show all the values by using this;

cur=head;
while(cur!=NULL)
{
print cur->item
cur=cur->next
};
Avatar of hthukral
hthukral

ASKER

Is it possible how I can I use it with Linked List using my while loop and how I can do it....I havent ever used Linked Lists before....

Thank You
But the question is I implemented how you said...I dont know write or wrong....now will it be possible to use these values to compare somethings in the file and change if reqd.....

If its possible can you tell me how to implement it exactly.....
Avatar of Kent Olsen
Hi hthukral,

Do you know how many items you may have to check in the chain?  Instead of building a linked list of objects over which you may have little control, you can always build a table of pointers to the items, provided you know the upper limit.

char *iptable[MAX_SIZE]
int ipcount;

ipcount = 0;
while(sp != NULL)
     {
          // Conversion from std::string to QString
          QString t(destination.c_str());
          if (sp == G.map[(G.find_node(G.map, source))])
          {
               ip1 = sp->name.c_str();
               iptable[ipcount++] = ip1;
          }
          else
          {
               ip2 = sp->name.c_str();
               iptable[ipcount++] = ip2;
          }
          sp = sp->next;
     }     // end of while


Good Luck!
Kent
Hi kdo

What do you mean by upper limit.......you mean how many objects....

The thing is I'm getting the result from Dijkstra Algorithm.....Ip1 tells me the first starting IP and then it goes into else loop and tells to which Ip1 is connected to and so on and which is the shortest path.....

Suppose you want to find the shortest path from 192.10.10.1 to 192.10.10.8

So when first time it goes into the while loop it gets ip1 which is 192.10.10.1.......then all the rest of the time it goes in else and tells me the next connected node...Suppose that is 192.10.10.5.....then it will tell to where this Ip is connected maybe to 192.10.10.8....

So our path will be 192.10.10.1 ---->192.10.10.5---->192.10.10.8...

Now when it is in the while loop all the time ip2 value changes, I need to know all the Ip addresses using this while loop...

Hopefully this makes it more clear...

Thanx Experts !!!

SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
When I'm doing this way...

Wont be ever more than 200 nodes never....

if (ipcount >= MAX_SIZE)
printf ("path too long, only the first %d nodes listed.\n", MAX_SIZE);
     for (idx = 0; idx < ipcount; idx++)
       printf ("node %s\n", iptable[idx]);

So, is it possible I will be able to use iptable to read the Values and compare in the file....thats my main purpose......

What I have to do when I get the result back check the correspoding IP's existence in the Topology file and do some things accordingly...I know how to do some STL's and play in the file...but without a for statement is there any way I can read the iptable...or ipcount.....

Thanx for the help !!! Hopefully ur next answer will close this question and help me proceed .....

Really Appreciate !!!
I implemented this ....and I had ip1 and ip2 as QStrings , as I m using QT.....Then I changed to string, it still has the error something like this

 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

This is the point where there is error:
iptable[ipcount++] = ip1;

Can you please tell what it will accept....

Thanx
iptable[ipcount++] = ip1;
// iptable is character array while ip1 is string value.

try this:
iptable[ipcount++] = ip1.c_str();

tell me if it works or not.
Nope it didnt work....

error C2440: '=' : cannot convert from 'const char *' to 'char *'
Conversion loses qualifiers

Please suggest......
oh.. i am lost. i confuse with the string and constant string.
i am not sure if my above comment is correct or not as i just saw the previous comment by Kdo.

another thing is you should learn linked list. it is quite interesting. some web that you can learn linked list:
http://www.fortunecity.com/skyscraper/false/780/linklist.html
http://richardbowles.tripod.com/cpp/linklist/linklist.htm
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=8021&lngWId=3
http://www.functionx.com/cpp/articles/linkedlist.htm

i give Kdo to answer the next question as he almost completed and i am in confusion now.

good luck.
Thnx for the tutorials. I'm in the middle of my project rite now and I want this to some how work.....I even dont know any way which will let me change from const char to char......

I even want to learn Linked Lists so for sure will read the articles....

Thanx
know what wrong already.
try
 strcpy(iptable[ipcount++] , ip1);
please include this :

#include <cstring>         //for strcpy()
or
#include<string.h>
How you sugegsted I did like that....
strcpy(iptable[ipcount++],ip1.c_str());

I have to give .c_str() to convert to const char

It compiled.....I started in debug mode
Then it went into exception on this line....

When It gets into assembly goes over my head......
main_loop:                          ; edx contains first dword of sorc string
        mov     [edi],edx           ; store one more dword
        add     edi,4               ; kick dest pointer

Thx
without the .c_str()

It comes up with an error...and this is the error
strcpy(iptable[ipcount++],ip1);

error C2664: 'strcpy' : cannot convert parameter 2 from 'std::string' to 'const char *'
haha.... include both and try
#include <string>    
#include <cstring>
strcpy(iptable[ipcount++],ip1.c_str());
Same thing....I think we are using Hit and Trial Method......
ip1 = sp->name.c_str();//A
if (ipcount < MAX_SIZE)
iptable[ipcount++] = ip1;
// just now i thought ip1 is already a constant string as A
This was the way which kdo suggested at the beginning when he provided with the code and this is the error..

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

Thanx
sorry for causing so much confusion.
no, i didnt say that one is answer. i just explained to you why am i keep changing with .c_str() or without
what i know
either
strcpy(iptable[ipcount++],ip1.c_str());
or
strcpy(iptable[ipcount++],ip1);
should get you the answer.
Sorry for not reading properly,

strcpy(iptable[ipcount++],ip1.c_str()); // Compiles but get an exception
or
strcpy(iptable[ipcount++],ip1); // Error...
should get you the answer.

I dont know how to proceed here....hopefully you can find some solution or kdo can tell some way.....
another Q what is yout ip1 data type? it is string right?
Yes, it is defined like this

string ip1;
string ip2;

then strcpy(iptable[ipcount++],ip1.c_str()); is ok
 but do you know what assembly error is that? i don't know assenbly language.

main_loop:                          ; edx contains first dword of sorc string
        mov     [edi],edx           ; store one more dword
        add     edi,4               ; kick dest pointer
This error is coming when I'm copying the string....If i remover strcpy and do something like this works fine....(iptable[ipcount++],ip1.c_str()); and even if i debug no exception....So i feel that this is some string copying assembly error, maybe it cannot copy it properly
//would you mind to try again? make all as string instead of character array
string iptable[MAX_SIZE+1]
int ipcount;

ipcount = 0;
while(sp != NULL)
     {
          // Conversion from std::string to QString
          QString t(destination.c_str());
          if (sp == G.map[(G.find_node(G.map, source))])
          {
               ip1 = sp->name.c_str();
               if (ipcount < MAX_SIZE)
                 iptable[ipcount++] = ip1;
          }
          else
          {
               ip2 = sp->name.c_str();
               if (ipcount < MAX_SIZE)
                 iptable[ipcount++] = ip2;
          }
          sp = sp->next;
     }     // end of while
     if (ipcount >= MAX_SIZE)
       printf ("path too long, only the first %d nodes listed.\n", MAX_SIZE);
     for (idx = 0; idx < ipcount; idx++)
       printf ("node %s\n", iptable[idx]);
// tell me the result
string iptable[MAX_SIZE+1];//with ;
//i don't have compiler here.. so cannot help you to try
I changed that to string from char and still the same thing....

iptable[ipcount++] = ip1;

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)


Thx
Looks like you are discussing a C++ stuff in C TA!

-ssnkumar
Not really we havent been to any OOP yet.....this is just a basic manipulation question....which started from a while loop....which I'm not able to figure out.....

Do you have any suggestions !!!
> Ip2 value is changing all the time.....how can I grab all the values...
What is this ip2 (its data type/declaration)?

If you just want to grab all the values (assuming that they are integers or string), you can put them in an array.

-ssnkumar
I'm getting some result from Algorithm, it gives the first ip in ip2, then goes in the loop again and then the second Ip in ip2 again.....

Ip2 is string.....can you tell me how can I do that....

Thx

Harsimrat
Hello ssnkumar

I'm using the Dijkstra Algorithm to get the result...If you want to see the Algorithm....there is an open question...I'm still waiting for answer for that question....sunnycoder was helping me for that....

https://www.experts-exchange.com/questions/21359401/Dijkstra-Algorithm.html

This is the open question...if you can take a look and provide a solution.......

Even i posted the above question in C++ also coz I wasnt sure where it belongs too...

Thx
Harsimrat
Get Extra Points

https://www.experts-exchange.com/questions/21359401/Dijkstra-Algorithm.html

Answer this Question here also and even in C++ and get bonus 250 points.....
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There is an error here on this line

str_array = (char **) realloc(str_array, size_of_array * sizeof(char));

It says str_array
Run-Time Check Failure #3 - The variable 'str_array' is being used without being defined.
I dont know what I was thinking when I posted my last comment ....
char **str_array;
Instead of this
char **str_array = 0;

It worked, went into loop first time successfully then got stuck here, getting an exception

str_array = (char **) realloc(str_array, size_of_array * sizeof(char));

The exception takes you in dbgheap.c

        for (i = 0; (base = _heap_regions[i]._regbase) != NULL &&
                i < _HEAP_REGIONMAX; i++)
        {
            if (pUserData >= base && pUserData <
                    (void *)(((char *)base)+_heap_regions[i]._currsize))
                return TRUE;
        }

        return FALSE;

#endif  /* WINHEAP */
} // This is where it shows it is breaking

Thx
Harsimrat
Change:
> str_array = (char **) realloc(str_array, size_of_array * sizeof(char));
To:
str_array = (char **) realloc(str_array, size_of_array * sizeof(char *));

> Run-Time Check Failure #3 - The variable 'str_array' is being used without being defined.
See in my previous post, I have defined it at the top.
So, you too have to do it like that.

-ssnkumar
> It worked, went into loop first time successfully then got stuck here, getting an exception
Did you make the correction that I gave in my previous post?

-ssnkumar
After doing the correction you sent it works perfectly fine...no exceptions....

I just have one question left...hopefully that will let me close this question and let me proceed.....

My main question ...due to which I changed from the while loop.....

NOw where is the result......and how can i access that result...

Secondly, I have this kind of topology in .txt file.......

Node1IpAddress         Node2IpAddress    Capacity
192.168.107.156        192.168.107.248   10
192.168.107.248        192.168.107.35     20
192.168.107.35        192.168.107.64     15
192.168.107.64        192.168.107.88     20
192.168.107.88        192.168.107.117   10
192.168.107.117        192.168.107.156   30
192.168.107.117        192.168.107.64     25
192.168.107.248          192.168.107.117   18
192.168.107.35        192.168.107.88     10
192.168.107.35        192.168.107.156   20
192.168.107.35        192.168.107.117   30

The result which comes from the Algorithm ...which you just figured out........

Suppose this is the result in that array  192.168.107.156 --->192.168.107.117 --->192.168.107.64 ...

How can I use this result and check this file.....for the corresponding IP's and my main purpose is to change capacities.....

I can change the capacities....but the only thing I dont know is ...the result what is obtained in an array how can I use it...

I know this question goes bit off what I started with.....I will increase this question point to 250...I will add 125..........

This thing is blocking me to proceed in my project.......

Thanx
Harsimrat
> NOw where is the result......and how can i access that result...
Your question is not clear to me.
You have the array. The strings are stored in it and you can always use it.

> but the only thing I dont know is ...the result what is obtained in an array how can I use it...
Where do you want to use it?

-ssnkumar
My result is an array....how can I get access to it........I want to use the strings from the array to compare with the Ip's in the .txt file.....

I have no clue how arrays work...I m learning C...so how can I see what is stored in the array...what is the procedure...


Thanx
Harsimrat
Here is a sample program to show how to compare the strings in the array with that in the .txt file:

main()
{
   FILE *fp = fopen("file.txt", "r");
   char **mystr;
   int size_of_array = 0;
   iny i;

   /* Fill up the mystr with the strings as shown in the previous posts */

   while (!feof(fp))
   {
        fscanf(fp, "%s%s%s", Node1ip, Node2ip, capacity);

        for (i = 0; i < size_of_array; i++)
        {
             if (strcmp(mystr[i], Node1ip) == 0)
            {
                 printf("%s matches with %dth element of array\n", Node1ip, i);
             }

             if (strcmp(mystr[i], Node2ip) == 0)
            {
                 printf("%s matches with %dth element of array\n", Node2ip, i);
             }
         }
    }
}

Hope this helps.......

-ssnkumar
In the above code there is a problem here...

if (strcmp(mystr[i], Node1ip) == 0)

and problem is with mystr[i] ...It comes with unhandled exception..... i tried all ways to make it work......but couldnt ....

is there any way we can change mystr to string and then compare it.........

Please suggest...

Thank You
Harsimrat
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
   
    char a[]="abc\0";
    string b="abc";
    string s;
    s=a;
    if(s==b)
    cout<<s;
    getch();
 return 0;  
}

try this example ...
mystr is declared like this:
> char **mystr;
So, that means you need to allocate memory to mystr and also to mystr[i].
This I have already showed you in my previous posts (See the realloc() for str_array)
If you don't do that, then the code:
> if (strcmp(mystr[i], Node1ip) == 0)
Will definitely give an "unhandled exception" and will crash.

Actually, I have left this portion for you to fill up.
See my comment:
>   /* Fill up the mystr with the strings as shown in the previous posts */
in the code.

Hope this clears your doubt.

-ssnkumar
Sorry, I never read that comment...Im just being stupid......My Mistake.....

Harsimrat
It's ok.
Are you clear on that now? Is it working?

-ssnkumar
Yes, it is working , urs also worked even the suggestion Kdo made also worked very well....I figured that out by changing in QT Strings...

So , for the points you guys helped me so much...So I'm increasing the points to 250 by adding 125 more to it.....and will split between you as the original question was 125 and you both deserve it....

I have one Dijkstra Question opened in C++/C and no one has actually answered me...so anyone of you can answer will give him atleast 500 points and will help me sort my stuff...

Thanx  a ton

Harsimrat