Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Another Problem In C++


  • Please log in to reply
1 reply to this topic

#1 Guest_danbrownlow_*

Guest_danbrownlow_*

  • Guests
  • OFFLINE
  •  

Posted 21 March 2007 - 07:46 AM

Hey again, i'm trying to get a new program to compile, and apparently theres a problem with the else statement.

It's saying that I need a primary expression before the else and that I need a ';' before the else on the 45th line. I can't see anything wrong with the program and I have checked the code with the book about 5 times and can't see any problems. Can anyone help me?

Here is the code:

#include <iostream>
#include <string>

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

int main()
{
	//ask for persons name
	cout << "Please enter your name: ";
	
	//read the name
	string name;
	cin >> name;
	
	//build the message
	const string greeting = "Hello, " + name + "!";
	
	//the number of blanks surrounding the greeting
	const int pad = 1;
	
	//total number of rows to write
	const int rows = pad * 2 + 3;
	const string::size_type cols = greeting.size() + pad * 2 + 2;
	
	//seperate output from input
	cout << endl;
	
	//write rows ROWS of output
	//invarient- we have written R rows so far
	for (int r = 0; r != rows; ++r) {
		string::size_type c = 0;
		
		 while (c != cols) {
				 // is it time to write the greeting?
				 if (r == pad + 1 && c == pad + 1){
					   cout << greeting;
					   c += greeting.size();
					   } else {
							 
							 //are we on the border
							 if (r == 0 || r == rows - 1 ||
							  c ==0 || c == cols - 1) {
									cout << "*";
									 else
									cout << " ";
							  ++c;
							  }
					   }
					   cout << endl;
					   }
				 return 0;
				 }
  
}


BC AdBot (Login to Remove)

 


m

#2 Guest_danbrownlow_*

Guest_danbrownlow_*

  • Guests
  • OFFLINE
  •  

Posted 21 March 2007 - 08:03 AM

Ok, I did it after looking for about half hour. I had and extra '{' after the (r == 0 || r == rows - 1 || c ==0 || c == cols - 1) bit. Sorry for wasting time!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users