This code hangs on me running. The problem is that "r" does not count right (not like it is directed under FOR expression). Does the problem lay beyond the code?

#include <iostream>
#include<string>


using namespace std;
// using std::cin;
// using std::endl;
// using std::cout;
// using std::string;


int main()
{
cout << "Please enter your name: ";
string name;
cin >> name;
const string greeting = name;
const int pad = 1;
const int rows  = pad*2 + 3;
const string::size_type cols = greeting.size() + pad*2 + 2;
cout << endl;


// int r = 0;
// while( r != rows )
for ( int r = 0; r != rows; ++r )
{


string::size_type c = 0;
while (c != cols )
{
// cout << "\nr= " << r << "\n";
// cout << "c= " << c << "\n";
// system("pause");
if ( r == pad + 1 && c == pad + 1 )
{
cout << greeting;
c += greeting.size();
}else
{
if ( r = 0 || r == rows-1 || c == 0 || c == cols-1 )
cout << "*";
else
cout << " ";
++c;
}
}
cout << endl;
// r=r+1;
}
system( "pause>null" );
return 0;
}

Recommended Answers

All 2 Replies

Your problem is in this line: if ([B] r = 0[/B] || r == rows-1 || c == 0 || c == cols-1 ) Do you see the error in the bold part? You're telling the compiler that r becomes 0. You're not asking if r equals 0. So r is reset to zero everytime the while-loop loops and so this statement: for ( int r = 0; r != rows; ++r ) will never reach it's end.
So changing the if statement to this: if ( r == 0 || r == rows-1 || c == 0 || c == cols-1 ) will probably solve your problem.
Niek

p.s. Nexttime you post code, please use codetags

thank you, niek_e.
I overlooked this one. Shame.

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.