0

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;
}

Edited by happygeek: fixed formatting

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Red Horse
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.