Hello,

I am having a problem with reading input from my web app. The data is being sent by JavaScript and I am using getline() to get the data. My problem is when I run it, I get an infinite loop. I do not know why this is happening.
A example of what is being sent to the server is:
4
6
1
1,2
3,4
done

JavaScript:

onClick="sendData(document.getElementById('graphN').selectedIndex + '\n'
 + document.getElementById('graphSize').value + '\n'
 + document.getElementById('graphD').selectedIndex + '\n'
 + document.getElementById('rc').value + '\n' 
 + 'done' + '\n'
 + '\n')">

C++ code with problem

getline(cin,name);
   graphName = atoi(name.c_str());
   getline(cin,size);
   sz = atoi(size.c_str());
   getline(cin,rest);

	  ti = atoi(input.c_str());
	  if(ti == 1)
         direct = 1;
      else
         indrect = 0;
   getline(cin,input);
   while(input != "done")
      {
	  cin.ignore(' ');
	  if(input.size() != 3)
	     exit(0);
	  vertices.push_back(input);
	  getline(cin,input);
	  }

Thank you in advance.

Try changing line 13 to

while(input != "done\n")

Try changing line 13 to

while(input != "done\n")

That shouldn't change a thing because getline() reads an input buffer up to "\n", but no including the '\n'.


Try removing line 15 " cin.ignore(' '); ", that should clear things up :)

That shouldn't change a thing because getline() reads an input buffer up to "\n", but no including the '\n'.


Try removing line 15 " cin.ignore(' '); ", that should clear things up :)

That fixed it! Thank you.

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.