Why doesn't my program display anything?

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2426 Views - Last Post: 01 March 2017 - 01:48 PM Rate Topic: -----

#1 klg52486  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 99
  • Joined: 01-December 16

Why doesn't my program display anything?

Posted 25 February 2017 - 12:45 PM

I can't figure out why my while loop doesn't do anything. When I enter a number, the program just terminates. What is happening? I don't have any syntax errors and I can't figure out where my logic is wrong.

int main()
{
	int size, 				// Number of asterisks on each side
		line_counter = 0,	// counts number of times loop executes
		star_counter = 0;
		
	cout 													<< endl
		 << "This program prints a solid diamond and a" 	<< endl
		 << "hollow diamond made of asterisks. You can" 	<< endl
		 << "choose how large you would like the diamond"	<< endl
		 << "to be by inputting how many asterisks you"		<< endl
		 << "would like on each side."				<< endl << endl
		 << "Please enter a number larger than two now:"	<< endl;
	cin	 >> size;
	
	while (size	>= line_counter++)
	{
		cout << " test text" << endl;
		
		while (line_counter >= star_counter++)
		{
			cout << "*";
		}
	}
	
	
	
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Why doesn't my program display anything?

#2 klg52486  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 99
  • Joined: 01-December 16

Re: Why doesn't my program display anything?

Posted 25 February 2017 - 12:54 PM

int main()
{
	int side_length, 				// Number of asterisks on each side
		line_counter = 0,	// counts number of times loop executes
		star_counter = 0;
		
	cout 													<< endl
		 << "This program prints a solid diamond and a" 	<< endl
		 << "hollow diamond made of asterisks. You can" 	<< endl
		 << "choose how large you would like the diamond"	<< endl
		 << "to be by inputting how many asterisks you"		<< endl
		 << "would like on each side."				<< endl << endl
		 << "Please enter a number larger than two now:"	<< endl;
	cin	 >> side_length;
	
	while (side_length	>= line_counter++)
	{
		cout << "*" << endl;
		
		while (line_counter >= star_counter++)
		{
			cout << "*";
		}
	}
	
	
	
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 klg52486  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 99
  • Joined: 01-December 16

Re: Why doesn't my program display anything?

Posted 25 February 2017 - 12:56 PM

int main()
{
	int side_length, 				// Number of asterisks on each side
		line_counter = 0,	// counts number of times loop executes
		star_counter = 0;
		
	cout 													<< endl
		 << "This program prints a solid diamond and a" 	<< endl
		 << "hollow diamond made of asterisks. You can" 	<< endl
		 << "choose how large you would like the diamond"	<< endl
		 << "to be by inputting how many asterisks you"		<< endl
		 << "would like on each side."				<< endl << endl
		 << "Please enter a number larger than two now:"	<< endl;
	cin	 >> side_length;
	
	while (side_length	>= line_counter++)
	{
		cout << "*" << endl;
		
		while (line_counter >= star_counter++)
		{
			cout << "*";
		}
	}
	
	
	
	return 0;
}



int main()
{
	int side_length, 				// Number of asterisks on each side
		line_counter = 0,	// counts number of times loop executes
		star_counter = 0;
		
	cout 													<< endl
		 << "This program prints a solid diamond and a" 	<< endl
		 << "hollow diamond made of asterisks. You can" 	<< endl
		 << "choose how large you would like the diamond"	<< endl
		 << "to be by inputting how many asterisks you"		<< endl
		 << "would like on each side."				<< endl << endl
		 << "Please enter a number larger than two now:"	<< endl;
	cin	 >> side_length;
	
	while (side_length	>= line_counter++)
	{
		cout << "*" << endl;
		
		while (line_counter >= star_counter++)
		{
			cout << "*";
		}
	}
	
	
	
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#4 klg52486  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 99
  • Joined: 01-December 16

Re: Why doesn't my program display anything?

Posted 25 February 2017 - 01:02 PM

Ugh sorry it posted three times!! My connection timed out and I didn't think it posted.

I fixed the variable I used that was a reserved word but it still doesn't do anything after I enter a number.
Was This Post Helpful? 0
  • +
  • -

#5 klg52486  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 99
  • Joined: 01-December 16

Re: Why doesn't my program display anything?

Posted 25 February 2017 - 01:12 PM

Sorry- the connection timed out and I didn't think the code posted, so it ended up posting 3 times.

It appears my explanation of the second piece of code didn't post, so if this is another repeat I apologize.

I fixed the reserved word I was using as a variable, but still nothing happens after I enter a number. The program just terminates.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon

Reputation: 5296
  • View blog
  • Posts: 16,509
  • Joined: 25-December 09

Re: Why doesn't my program display anything?

Posted 25 February 2017 - 01:16 PM

What variable was a reserved word (note: size is not a reserved word if that is what you meant)?

What operating system are you using?

What compiler?

Note: The code you provided will not compile since it is missing required header files and you are not properly scoping the std:: namespace.

After fixing the header issues your program compiles and produces output on my system.


Jim
Was This Post Helpful? 1
  • +
  • -

#7 klg52486  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 99
  • Joined: 01-December 16

Re: Why doesn't my program display anything?

Posted 25 February 2017 - 01:20 PM

My professor asked us to compile with g++ -Wall diamond.cpp -o diamond

When I compile with just g++ it works. hmm. obviously its not the right output, but seeing what it does it kind of important for me to figure out how to get it to do what I want lol. Any idea why he wants us to use g++ -Wall diamond.cpp -o diamond and why that would prevent it from working?

I'm using a unix server. Other than that I'm not sure because I just remote connect to the school server.
Was This Post Helpful? 0
  • +
  • -

#8 klg52486  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 99
  • Joined: 01-December 16

Re: Why doesn't my program display anything?

Posted 25 February 2017 - 01:29 PM

Oh, and why was size highlighted in pink if not a reserved word?
Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1849
  • View blog
  • Posts: 6,646
  • Joined: 19-February 09

Re: Why doesn't my program display anything?

Posted 25 February 2017 - 01:36 PM

On unix type systems it is often important to flush the output stream.

Near the end you can add an endl or flush.

cout << endl;




The pink coloring is just this forum's coloring and probably covers all the languages. Some language may have size reserved.
Was This Post Helpful? 1
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon

Reputation: 5296
  • View blog
  • Posts: 16,509
  • Joined: 25-December 09

Re: Why doesn't my program display anything?

Posted 25 February 2017 - 03:33 PM

Quote

Any idea why he wants us to use g++ -Wall diamond.cpp -o diamond and why that would prevent it from working?

The -Wall flag tells the compiler to warn for problems with most of the common problems, the diamond.cpp is the name of the file you're trying to compile, and the -o diamond flag tells g++ what to call the executable file. Are you sure you named your source file diamond.cpp and not something else?

You really should be using, IMO, the following minimal switches g++ -g -Wall -Wextra -pedantic-errors -std=c++11 diamond.cpp -o diamond

This tells g++ to place debugging symbols in your executable so you can run the program with your debugger and adds quite a few more "basic" error/warning diagnostics and the -pedantic-errors tells the compiler not to accept any of the compiler specific "hacks".

Quote

On unix type systems it is often important to flush the output stream.

When the program all streams should be automatically flushed when they are destructed.

Jim

This post has been edited by jimblumberg: 25 February 2017 - 03:34 PM

Was This Post Helpful? 2
  • +
  • -

#11 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1989
  • View blog
  • Posts: 5,313
  • Joined: 27-December 05

Re: Why doesn't my program display anything?

Posted 25 February 2017 - 05:00 PM

Quote

When I compile with just g++ it works


Just to be clear, are you saying that you were able to compile that code without any #include and without any using namespace statements?

g++ what? Exactly what command did you run?

Also, out of curiosity, what's the output if you run
g++ --version
Was This Post Helpful? 1
  • +
  • -

#12 klg52486  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 99
  • Joined: 01-December 16

Re: Why doesn't my program display anything?

Posted 28 February 2017 - 11:06 AM

View Postr.stiltskin, on 25 February 2017 - 05:00 PM, said:

Quote

When I compile with just g++ it works


Just to be clear, are you saying that you were able to compile that code without any #include and without any using namespace statements?

g++ what? Exactly what command did you run?

Also, out of curiosity, what's the output if you run
g++ --version


Sorry for the confusion- it compiled using g++ diamond.cpp, and I do have #include <iostream> and using namespacestd at the top. Then I just type a.out to run the program. Now that I am done with the program it compiles just fine using Wall as well. Would you still like me to try the --version thing? What does that do?? Since it compiles using Wall I'm guessing the --version wouldn't be useful. And now I wonder if putting endl at the bottom would have helped. I'll remember that if something like that happens again.

#include <iostream>
using namespace std;


int main()
{
	int side_length, 		// Number of asterisks on each side
		line_counter,		// counter for main loop
		counter,			// counter for inner loops
		spaces;				// determines number of spaces printed

	bool valid_data;		// tests for input less than 2

	cout 													<< endl
		 << "This program prints a solid diamond and a" 	<< endl
		 << "hollow diamond made of asterisks. You can" 	<< endl
		 << "choose how large you would like the diamond"	<< endl
		 << "to be by inputting how many asterisks you"		<< endl
		 << "would like on each side."				<< endl << endl;

// Validates data and starts over if data is less than 2		 
while (valid_data == false)
{	

	cout << "Please enter a number (two or larger):"<< endl;
	cin	 >> side_length;

	cout << endl;
	
	if (side_length >= 2)
	{
		valid_data = true;
	}
	else
	{
		cout << "************************************" << endl
			 <<	"*   You entered a number smaller   *" << endl
			 << "*   than two. Please try again     *" << endl
			 <<	"************************************" << endl;
	}
}

	spaces = side_length - 1; // number of spaces on first line
	
	// Main loop for top half. Ends when rows = input entered
	for (line_counter = 1; line_counter <= side_length; line_counter++)
	{
		// prints spaces to the left of the asterisks
		for (counter = 1; spaces >= counter; counter++)
		{
			cout << " ";
		}
		
		spaces--;   // decrements spaces so one less space prints each iteration
		
		// prints one additional asterisk each iteration
		for (counter = 1; counter <= 2 * line_counter - 1; counter++)
		{
			cout << "*";
		}
		
		cout << endl;
	}
	
	// number of spaces on first printed line of lower half
	spaces = 1;
	
	// main loop for lower half. ends when counter == user input - 1
	for (line_counter = 1; line_counter <= side_length - 1; line_counter++)
	{
		// prints spaces to the left of the astersisks
		for (counter = 1; spaces >= counter; counter++)
		{
			cout << " ";
		}
		
		spaces++; // increments to print an additional space each iteration
		
		// prints one less asterisk each iteration
		for (counter = 1; counter <= 2 * (side_length - line_counter) - 1; counter++)
		{
			cout << "*";
		}
		
		cout << endl;
	}	
	
	// number of spaces on first line
	spaces = side_length - 1;
	
	// Main loop for top of hollow triangle. Ends when rows == user input
	for (line_counter = 1; line_counter <= side_length; line_counter++)
	{
		// Prints spaces to the left of the diamond.
		for (counter = 1; spaces >= counter; counter++)
		{
			cout << " ";
		}
		
		spaces--; // decrements so one less space is printed on each line
		
		cout << "*"; // prints one asterisk per line on the left side of the diamond
		
		// prints spaces to make the center of the diamond
		for (counter = 1; counter <= 2 * line_counter - 3; counter++)
		{
			cout << " ";
		}
		
		//Prints one asterisk per line on the right side, except top and bottom line
		if (line_counter > 1)
		{
			cout << "*";
		}
		
		cout << endl;
	}
		
	spaces = 1; // space printed on first line on lower half
	
	// main loop for lower half. ends when rows == user input - 1
	for (line_counter = 1; line_counter <= side_length - 1; line_counter++)
	{
		//prints spaces to the left of diamond
		for (counter = 1; spaces >= counter; counter++)
		{
			cout << " ";
		}
		
		spaces++; // increments to print an additional space per line
		
		cout << "*";
		
		// prints spaces for center of diamond
		for (counter = 1; counter <= 2 * (side_length - line_counter) - 3; counter++)
		{
			cout << " ";
		}
		
		// prints asterisks on right side except top and bottom
		if (line_counter < side_length -1)
		{
			cout << "*";
		}
		
		cout << endl;
	}	
		
	return 0;
}



Was This Post Helpful? 0
  • +
  • -

#13 no2pencil  Icon User is online

  • Professor Snuggly Pants
  • member icon

Reputation: 6543
  • View blog
  • Posts: 30,648
  • Joined: 10-May 07

Re: Why doesn't my program display anything?

Posted 28 February 2017 - 11:25 AM

View Postklg52486, on 25 February 2017 - 03:20 PM, said:

I'm using a unix server. Other than that I'm not sure because I just remote connect to the school server.


What is the output of uname -a from the command line?

you have nothing outside of your while statement, so if your while statement conditional was NOT EQUAL to, nothing would happen. On line 22 you are checking that the variable valid_data is equal to false. On line 21, print the value of the variable valid_data.
Was This Post Helpful? 1
  • +
  • -

#14 jimblumberg  Icon User is offline

  • member icon

Reputation: 5296
  • View blog
  • Posts: 16,509
  • Joined: 25-December 09

Re: Why doesn't my program display anything?

Posted 28 February 2017 - 11:55 AM

Quote

What does that do??

g++ --version will report the version of g++ that will be used to compile your program.

Quote

Now that I am done with the program it compiles just fine using Wall as well.

What about with the other suggested compile flags. IMO, -Wall does not produce enough diagnostics to produce stable programs.

Jim
Was This Post Helpful? 2
  • +
  • -

#15 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1989
  • View blog
  • Posts: 5,313
  • Joined: 27-December 05

Re: Why doesn't my program display anything?

Posted 28 February 2017 - 01:03 PM

I only meant for you to run g++ --version one time to find out your compiler version in case that might shed any light on your questions -- now or future.

The command uname -a will identify your operating system and processor which might also be useful to know.

By the way, on Unix and Linux systems you can find out information about what a command does by running
man [command]   // replace [command] with the actual name of the command

which brings up a manual page ("manpage") describing the command and its options.

I agree with jimblumberg that you should be using additional compiler flags always (although I personally use -pedantic rather than -pedantic-errors). However, that should have nothing to do with the problem you asked about in post #1 (and #2, and #3). But also, there should have been no difference between g++ diamond.cpp and g++ -Wall diamond.cpp -o diamond other than the name of the executable that it produced. So I suspect that either somehow you weren't actually compiling, or running, that code. You might try it again just as an experiment, bearing in mind that the first one produces a.out as the executable whereas the second produces diamond as the executable.

Regarding your revised code (post #12) you should be initializing valid_data to false on line 12 because otherwise there's no guarantee that it will start out as false.
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2