Why doesn't it write out the greeting???

Hi everyone, I'm trying to teach myself c++, and am currently using the book Accelerated C++ by Andrew Koenig and Barbara E. Moo. This is one of the programs in the book, but I can't seem to make it write out the greeting. Please help.


//Alle mulige ting til programmet
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

//Her kan der, hvis det er nødvændigt defineres nogle globale variabler


int main()
{

cout << "Please enter your first name: ";

string name;
cin >> name;

const string greeting = "Hello, " + name + "!";

const int pad = 1;

const int rows = (pad*2) + 3;
const string::size_type cols=greeting.size()+(pad*2)+2;

cout << endl;

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

string::size_type c=0;

while (c!=cols){

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

system("pause>nul");

return 0;

}
you used an assignment sign where you need a equality sign. in other words you wrote = instead of ==.


change this

1
2
3
4
5
if ((r==0) || (r=(rows-1))||(c==0)||(c==(cols-1))){ // error in this line
cout <<"*";}
else{
cout << " ";
}


to

1
2
3
4
5
if ((r==0) || (r==(rows-1))||(c==0)||(c==(cols-1))){ 
cout <<"*";}
else{
cout << " ";
}


i also had to add <stdlib.h> for the system command to work on a non microsoft compiler
Last edited on
Thanks :)
Topic archived. No new replies allowed.