• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

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....
0
hthukral
Asked:
hthukral
  • 26
  • 15
  • 9
  • +1
2 Solutions
 
beryl666Commented:
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?
0
 
beryl666Commented:
after that you can show all the values by using this;

cur=head;
while(cur!=NULL)
{
print cur->item
cur=cur->next
};
0
 
hthukralAuthor Commented:
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
hthukralAuthor Commented:
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.....
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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
0
 
hthukralAuthor Commented:
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 !!!

0
 
Kent OlsenData Warehouse Architect / DBACommented:

Ok.  Then method that I outlined will work.  (It might need modifying depending upon whether the returned addresses are static.)

#define MAX_SIZE 200   /*  Allow up to 200 nodes in the list.  This should be enough for any path.  */

char *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]);



One other thing.  c_str() is a C++ function that returns the C address of a string (or object).  You're obviously converting this function from C++ to C (or creating a C subroutine for a C++ program).  This may be an issue before you're done.


Kent
0
 
hthukralAuthor Commented:
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 !!!
0
 
hthukralAuthor Commented:
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
0
 
beryl666Commented:
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.
0
 
hthukralAuthor Commented:
Nope it didnt work....

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

Please suggest......
0
 
beryl666Commented:
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.
0
 
hthukralAuthor Commented:
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
0
 
beryl666Commented:
know what wrong already.
try
 strcpy(iptable[ipcount++] , ip1);
0
 
beryl666Commented:
please include this :

#include <cstring>         //for strcpy()
or
#include<string.h>
0
 
hthukralAuthor Commented:
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
0
 
beryl666Commented:
without the .c_str()

0
 
hthukralAuthor Commented:
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 *'
0
 
beryl666Commented:
haha.... include both and try
#include <string>    
#include <cstring>
strcpy(iptable[ipcount++],ip1.c_str());
0
 
hthukralAuthor Commented:
Same thing....I think we are using Hit and Trial Method......
0
 
beryl666Commented:
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
0
 
hthukralAuthor Commented:
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
0
 
beryl666Commented:
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.
0
 
hthukralAuthor Commented:
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.....
0
 
beryl666Commented:
another Q what is yout ip1 data type? it is string right?
0
 
hthukralAuthor Commented:
Yes, it is defined like this

string ip1;
string ip2;

0
 
beryl666Commented:
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
0
 
hthukralAuthor Commented:
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
0
 
beryl666Commented:
//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
0
 
beryl666Commented:
string iptable[MAX_SIZE+1];//with ;
//i don't have compiler here.. so cannot help you to try
0
 
hthukralAuthor Commented:
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
0
 
ssnkumarCommented:
Looks like you are discussing a C++ stuff in C TA!

-ssnkumar
0
 
hthukralAuthor Commented:
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 !!!
0
 
ssnkumarCommented:
> 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
0
 
hthukralAuthor Commented:
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
0
 
hthukralAuthor Commented:
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....

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21359401.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
0
 
hthukralAuthor Commented:
Get Extra Points

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_21359401.html

Answer this Question here also and even in C++ and get bonus 250 points.....
0
 
ssnkumarCommented:
char **str_array;
int size_of_array = 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();
               str_array = (char **) realloc(str_array, size_of_array * sizeof(char));
               str_array[size_of_array] = (char *) malloc(strlen(sp->name.c_str()));
               strcpy(str_array[size_of_array], sp->name.c_str());
               size_of_array++;
          }
          else
          {
         
               ip2 = sp->name.c_str();
               
               str_array = (char **) realloc(str_array, size_of_array * sizeof(char));
               str_array[size_of_array] = (char *) malloc(strlen(sp->name.c_str()));
               strcpy(str_array[size_of_array], sp->name.c_str());
               size_of_array++;
          }

          sp = sp->next;

     }     // end of while

Try this and let me know if this works for you.

-ssnkumar
0
 
hthukralAuthor Commented:
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.
0
 
hthukralAuthor Commented:
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
0
 
ssnkumarCommented:
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
0
 
ssnkumarCommented:
> 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
0
 
hthukralAuthor Commented:
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
0
 
ssnkumarCommented:
> 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
0
 
hthukralAuthor Commented:
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
0
 
ssnkumarCommented:
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
0
 
hthukralAuthor Commented:
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
0
 
beryl666Commented:
#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 ...
0
 
ssnkumarCommented:
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
0
 
hthukralAuthor Commented:
Sorry, I never read that comment...Im just being stupid......My Mistake.....

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

-ssnkumar
0
 
hthukralAuthor Commented:
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
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 26
  • 15
  • 9
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now