I am trying to build a triangle, with a user entered base and height.
When these entered values are different (base!=height), the program goes haywire and gets stuck in the triangle draw loop.
I've tried altering the code a couple of times, but please treat me as a programming novice.
//BUILD TRIANGLE//
#include <string>
#include <iomanip>
#include <iostream>
int main()
{
std::cout << "\nEnter base and height:\n";
int height{0}; int base{0};
std::cin >> base >> height;
std::string bottom(base, '*');
std::string top = "*";
int middlerows = height - 1;
int middlespacechars;
std::cout << top << std::endl;
for (middlespacechars = 0;
middlerows != 1 || middlespacechars != base - 2;
++middlespacechars, --middlerows) {
std::string middlespace(middlespacechars, ' ');
std::cout << "*" << middlespace << "*\n";
}
std::cout << bottom << "\n" << std::endl;
std::cout << "^TRIANGLE\n";
std::cout << "BASE = " << base << std::endl;
std::cout << "HEIGHT = " << height << std::endl;
std::cout << "goodbye" << "\n" << std::endl;
}
The output is totally haywire, with asterisks across the screen in no discernible shape.
When I put in values where base=height, though, a pretty little right angle triangle pops up.
With your code, you can only draw well triangles which have base equal to height.
If you change stop condition in your for loop, you can get what you probably want to get:
for (middlespacechars = 0; middlerows != 1 || middlespacechars != base - 2; ++middlespacechars, --middlerows) {
... into ...
for (middlespacechars = 0; middlerows > 1 || middlespacechars < base - 2; ++middlespacechars, --middlerows) {
It was huge probability that if base and height are different then stop condition will not be achieved. For loop in your code will stop if middlerows will be 1 and middlespacechars will be base - 2 at the same moment.
Test it here.
//C++ program to display hollow star pyramid
#include<iostream>
using namespace std;
int main()
{
int rows, i, j, space;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
//for loop to put space in pyramid
for (space = i; space < rows; space++)
cout << " ";
//for loop to print star
for(j = 1; j <= (2 * rows - 1); j++)
{
if(i == rows || j == 1 || j == 2*i - 1)
cout << "*";
else
cout << " ";
}
cout << "\n";
}
return 0;
}
I want to cout an array as a row vector but when I write:
int main() {
int B[3]={0};
for (int w = 0; w <2; w++) {
cout <<"B="<<" "<< B[w] << " ";
}
cout << endl;
return 0;
}
The output is B=0 B=0
But I want output to be like:
B=(0 0)
For a fixed size array of only I would probably even prefer a oneliner like this, because I can read it at first glance:
cout << "B=(" << B[0] << " " << B[1] << " " << B[2] << ")\n";
For a container B with a dynamic or very high number of elements n, you should probably do something like this:
cout << "B=(";
if(n > 0)
{
cout << B[0];
// note the iteration should start at 1, because we've already printed B[0]!
for(int i=1; i < n; i++)
cout << ", " << B[i]; //I've added a comma here, so you get output like B=(0, 1, 2)
}
cout << ")\n";
This has the advantage, that no matter what number of elements, you don't end up with trailing commas or unwanted whitespace.
I'd reccommend making a generic (template) function for the purpose of printing array/std::vector content anyways - it's really useful for debugging purposes!
int main() {
int B[3] = { 0 };
cout << "B=(";
for (int w = 0; w < 3; w++) {
cout << B[w];
if (w < 2) cout << " ";
}
cout << ")" << endl;
return 0;
}
Output should be now:
B=(0 0 0)
The simplest way to do this is:-
#include<iostream>
using namespace std;
int main()
{
int B[3]={0};
cout << "B=(";
for (int w = 0; w < 3; w++)
{
cout << B[w] << " ";
}
cout << ")" << endl;
return 0;
}
the output will be B= (0 0 0 )
You can try this one if you want:
#include <iostream>
using namespace std;
int main() {
int B[3]={0};
cout << "B=(";
for (int w = 0; w <2; w++) {
cout << B[w];
if(w != 1) cout << " ";
}
cout << ")" << endl;
cout << endl;
return 0;
}
The output is:
B=(0 0)
The line if(w != 1) checks whether you 've reached the last element of the array. In this case the last index is 1, but in general the if statement should be: if(w != n-1) where n is the size of the array.
I am running a program in c++ which prints the multiplication table from 1 to 40 but it starts from 13*10=130 to 40 so whats the reason behind this?
Below is the formatted version of the code you posted:
#include<iostream>
using namespace std;
int main() {
for (int i = 1; i <= 40; i++) {
for (int j = 1; j <= 10; j++) {
cout << i << " * " << j << " = " << i*j << endl;
}
cout << endl;
}
return 0;
}
It starts printing at 13 * 10. What's the reason for this?
Notably, we can see the variables of both loops (i and j) are initialized to 1 when the loop starts. Because of this, you would be right in expecting the first loop-through to print 1 * 1 = 1.
This suggests, as PRIME has pointed out, that the environment (such as the Windows Console) that you are printing to may not have a large enough buffer to store and display the 440 lines of output the program will try to print.
How can I get around this?
You can try re-sizing your printing environment's internal buffer (if it allows) to allow for 440 lines of print. In MS-DOS, for example, you can manually change this by right-clicking the title-bar, going into Properties, then the Layout tab, and changing the Screen Buffer width and height to values that suit.
Alternatively, you could conserve print-space by replacing endl statements with regular spaces, a la:
for (int i = 1; i <= 40; i++) {
for (int j = 1; j <= 10; j++)
cout << i << " * " << j << " = " << i*j << ' ';
}
You also have the option of outputting to a file instead of your current printing environment:
#include <fstream>
using namespace std;
int main() {
ofstream Output("Output.txt"); //Creates a file "Output.txt"
if (Output.is_open()) { //If the file is open, proceeed
for (int i = 1; i <= 40; i++) {
for (int j = 1; j <= 10; j++)
Output << i << " * " << j << " = " << i*j << '\n';
Output << '\n'; //^^^Write multiplication table to the file
}
}
return 0;
}
I am making a 20 questions game in C++ and have everything working, except for the displayWords function. The code I currently have keeps breaking. Any explanation would be appreciated! Thank you!
void displayWords()
{
int x = 0;
string words[50] = {"LCHS","Shark","Pencil","Pizza","New York","Fish","Car","Ice Cream","Los Angeles","Bird","Basketball","Fried Chicken",
"Dog","Tiger","Penguin","Plane","Rock","Barbecue Sauce","Mustard","Ketchup","Hot sauce","Peppers","Salt","Tacos","Shrimp","Pickels",
"Tomatos","Bannanas","Burger","Computer","Iphone","Motorcycle","Bicycle","Skateboard","Lightbulb","Golf Ball","Surfboard","Luggage",
"Rollercoaster","Cat","Lion","Cockroach","Grasshopper","Beach","Theme Park","Swimming Pool","Bowling Ally","Movie Theater","Golf Course","Shopping Mall"};
cout << "The following list of words are what the computer is capable of guessing" << endl;
cout << endl;
while(x < 50)
{
for (int y = 0; y <= 5; y++)
{
cout << words[x] << ", ";
if(x<50)
x++;
}
cout << endl;
}
}
I would like it to display the list of 50 words in an organized fashion.
By example, as:
for( int x = 0; x<sizeof(words)/sizeof(*words); x++ ) {
if( x%5==0 ) cout << endl; else cout << ", ";
cout << words[x];
}
take into account the problematic of the array's size calculation: see this link How do I find the length of an array?
If I understand correctly, you want your list displayed as 5 columns. Simplest way, use a nested for loop and proper formatting with std::setw (must #include <iomanip>):
for(size_t i = 0; i < 10; ++i)
{
for(size_t j = 0; j < 5; ++j)
{
std::cout << std::setw(20) << std::left << words[i * 5 + j];
}
std::cout << std::endl;
}
Your actual loop is incorrect, as it will lead to repetitions.
Maybe I'm not interpreting your question correctly but if you want to just print out the 50 words then you can use something like the code below. Not sure of the reason that the nested for loop iterating y was there.
Edit
void displayWords()
{
int x;
string words[50] = {"LCHS","Shark","Pencil","Pizza","New York","Fish","Car","Ice Cream","Los Angeles","Bird","Basketball","Fried Chicken",
"Dog","Tiger","Penguin","Plane","Rock","Barbecue Sauce","Mustard","Ketchup","Hot sauce","Peppers","Salt","Tacos","Shrimp","Pickels",
"Tomatos","Bannanas","Burger","Computer","Iphone","Motorcycle","Bicycle","Skateboard","Lightbulb","Golf Ball","Surfboard","Luggage",
"Rollercoaster","Cat","Lion","Cockroach","Grasshopper","Beach","Theme Park","Swimming Pool","Bowling Ally","Movie Theater","Golf Course","Shopping Mall"};
cout << "The following list of words are what the computer is capable of guessing" << endl;
cout << endl;
for(x = 0; x < words.size();x++)
{
cout << words[x]<< ", ";
}
}
Also some information on how the code is breaking, like are any errors being thrown or has debugging caused issues so far?
I am having a brain shock right now so I wanted to ask very simple question.
Currenly, I am trying to print out starts like this
when input is 7 , the output is
*
**
*
**
*
**
*
and here my code is , it prints 14 times instead of 7 or when I put N/2 it doesnt print the odd number.
#include <iostream>
using namespace std;
int main () {
int N;
cout << " Please enter N " ;
cin >> N;
for (int i = 0; i < N ; i++) {
cout << "*" << endl;
for (int j = 0; j < 2; j++) {
cout << "*" ;
}
cout << endl;
}
}
For each N you are printing two lines, with single * and another with two *. Instead just print single line with either one or two star based on the line is odd or even.
#include <iostream>
int main ()
{
unsigned int N;
cout << " Please enter N " ;
cin >> N;
for(unsigned int i = 0; i < N; ++i)
{
if(i%2 == 0)
{
std::cout << "*" << std::endl;
}
else
{
std::cout << "**" << std::endl;
}
}
}
(Untested code)
Can't you just go like this :
for (int i = 0; i < N ; i++) {
if (i%2 == 0)
{
cout << "**" << endl;
}
else
{
cout << "*" << endl;
}
}
In your case, for each of your N iterations, you print , jump to a new line, print *, and then jump to a new iteration. So 14 lines when N is 7.
It's because each time the first for loop runs, the second loop also runs. You can't print out both * and ** and expect it to print N times (it will always print 2 * N times). You need to print either * or **, but not both at the same time. Simple example:
bool alternate = false;
for (int i = 0; i < N ; i++) {
if (alternate) {
cout << "*" << endl;
} else {
cout << "**" << endl;
}
alternate = !alternate;
}
You could remove the alternate variable and check if i is even or odd (with something like i & 1), but I used the alternate variable to help make it clearer.
For each complete iteration of your outer loop the following is printed:
*
**
If you run that loop 7 times then you'll get 14 rows. try this instead, no need for the inner loop:
for (int i = 0; i < N ; i++) {
cout << "*" << endl;
cout << "**" << endl;
}