C++ Displaying An Organized List of Words - c++

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?

Related

How can I print 2D arrays with four columns

I am struggling with printing an array with 4 rows and 4 columns, when I initialized the array and entered all the values. Then, I used for loop to get all the values together so I can print them. But I get is an array that companied all the values in one row.
I have attached the output when I run the code.
Here is a portion of my code, it is long code but I am struggling in specific part:
#include <iostream>
using namespace std;
int main()
{
cout << "The martix before I flipped it: " << endl;
cout << endl;
int array[4][4] = { 16,3,2,13,5,10,11,8,9,6,7,12,4,5,14,1 };
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << array[i][j] << " ";
}
}
return 0;
The standard output utility std::cout is a stream from the stl and, as such, its << operator does not usually automagically append a linebreak.
Which is quite practical since, otherwise, you would not be able to print multiple numbers on a single line.
That being, said, you'll need to add the linebreak manually, like so :
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
std::cout << array[i][j] << " ";
}
std::cout << std::endl;
}
Alternatively, you can consider printing lines 4 at a time since your matrix is of constant size :
for (int i = 0; i < 4; i++) {
std::cout << array[i][0] << " "
<< array[i][1] << " "
<< array[i][2] << " "
<< array[i][3] << " " << std::endl;
}
Have a great day,

Fix array not properly being assigned

I am trying to assign a specific part of an array a new value, but it doesn't seem to be inserting the new value into the array.
char matrix[20][8] = {/*160 * '#'*/};
void Draw() {
system("CLS");
cout << "Welcome to Primitive Pong v1.0!" << endl;
for (int i = 0; i < 8; i++) {
cout << endl;
for (int j = 0; j < 20; j++) {
cout << matrix[i][j] << " ";
}
}
}
while (gameOver == false) {
matrix[10][4] = 'O';
Draw();
this_thread::sleep_for(chrono::milliseconds(1000));
}
I expect this to output a grid of 160 "#" with a "O" near the middle, but instead it just prints 160 "#". I am trying to make a game of console pong. I have tried using 'matrix[10][4] = {'O'};, but that does nothing different.
The problem is that you declare matrix[20][8] but then you access it as if its dimensions are [8][20] instead.
The total is the same but the access doesn't work correctly and, unfortunately, C++ will not check about this kind of mistake. Changing the code to
cout << matrix[j][i] << " ";
should make things work a you expect.

C++ Receiving error that the function was not declared in this scope [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
This is the porgram I am writing for my university's Assignment. I have written all the codes that takes 10 integers from user, and asks user to press 1 to show odd integers in ascending order from the list or 2 to show the even integers in ascending order from the list.
Well, I have declared and defined the Bubble Sorting function before the main() function in program, and used the function later in main() to sort the even and odd numbers in ascending order. But, I am still getting the error that the function was not declared in this scope even when I have declared it at the top. I tried all possible things I could do. Kindly help me what should I do? Below is my code
#include <iostream>
#include <conio.h>
using namespace std;
void BuubleSort_Function(int [], int);
void BuubleSort_Function(int arr[], int arrSize)
{
int extraMem;
for(int i = 0; i < arrSize; i++)
{
for(int arrIndex = 0; arrIndex < arrSize - 1; arrIndex++)
{
if(arr[arrIndex] > arr[arrIndex+1])
{
extraMem = arr[arrIndex];
arr[arrIndex] = arr[arrIndex+1];
arr[arrIndex+1] = extraMem;
}
}
}
}
main()
{
int num[10], i, even[10], odd[10], inputOpt, totalEvens = 0, totalOdds = 0;
system("cls");
cout << "Please enter 10 integers: " << endl;
for(i = 0; i < 10; i++)
{
cin >> num[i];
}
cout << endl << endl << endl << "1. Show odd numbers in ascending order and their total numbers" << endl;
cout << "2. Show even numbers in ascending order and their total numbers" << endl;
do
{
cout << endl << "Enter 1 for the first option or 2 for the second option: ";
cin >> inputOpt;
if(inputOpt != 1 && inputOpt != 2)
{
cout << "Wrong Input! Please enter the correct input value";
}
}
while(inputOpt != 1 && inputOpt != 2);
if(inputOpt == 1)
{
for(i = 0; i < 10; i++)
{
if(num[i] % 2 == 1)
{
odd[totalOdds] = num[i];
totalOdds++;
}
}
BubbleSort_Function(odd,totalOdds);
cout << endl << "The total numbers of Odds Integers are " << totalOdds;
cout << endl << "The Integers arranged in Ascending Order:" << endl;
for(i = 0; i < totalOdds; i++)
{
cout << odd[i] << "\t";
}
}
if(inputOpt == 2)
{
for(i = 0; i < 10; i++)
{
if(num[i] % 2 == 0)
{
even[totalEvens] = num[i];
totalEvens++;
}
}
BubbleSort_Function(even,totalEvens);
cout << endl << "The total numbers of Odds Integers are " << totalEvens;
cout << endl << "The Integers arranged in Ascending Order:" << endl;
for(i = 0; i < totalEvens; i++)
{
cout << even[i] << "\t";
}
}
}
A simple typo: the function is declared and implemented as BuubleSort_Function.
You attempt to call it using BubbleSort_Function.
The compiler error messages are very helpful. Do learn to interpret them.
(Finally, standard C++ requires you to mark main() as returning an int - a C++ compiler will insert an implicit return 0 into main if it's missing. Some compilers - especially ones for embedded systems - drop this requirement but that is a deviation from the standard.).
Defination - BuubleSort_Function
Caller - BubbleSort_Function
Compare the 5th line and the 7th line with the 64th line,and you will find what the problem is.Just a simple typo.

How to do this c++ looping

I am sorry i'm new learner in programming. I would like to ask for help where i want to display a range number start from 200 until 400 but it should not display number 250 in it.
This is what i have done.
int main () {
for (int i=200; i<=400; i++) {
std::cout << "value of i: " << i << endl;
}
return 0;
}
I was sucessfuly display the range number, but it display all number.
If you want to avoid the if for every iteration, you could of course also split it into two loops:
for (int i = 200; i < 250; ++i)
std::cout << "value of i: " << i << endl;
for (int i = 251; i <= 400; ++i)
std::cout << "value of i: " << i << endl;
Pretty simple, but simple can be very good in this business.
If 250 is the only number you don't want to display in your range, you need to use and if statement. This statement would be stating that you should print number only if they are different from 250.
for (int i=200; i<=400; i++) {
if (i != 250) { // if my current number i is different from 250 I print it
std::cout << "value of i: " << i << std::endl;
}
}

How can I get the array to show the previous list of array content before the next input?

I need the user input to be saved into my array and then output the array before the user inputs the next time. I have been moving things around different ways but cannot seem to get them to perform properly. I tried to cut down the code to the two functions I am having issues with.
void PlayGame()
{
const int HighestNum = 50;
const int LowestNum = 1;
int RandomNumber = LowestNum + rand() % HighestNum; //set for better random results
cout << "Guess the random number between " << LowestNum << " and " << HighestNum << "!\n\n";
const int attempts = 15;// limits the attempts to guess the random number to 15
int Guess [attempts] = {};
cout << "Enter your guess " << endl;
for (int count = 0; count < attempts; count++)
{
cin >> Guess[count];
int z = RandomNumber, y = Guess[count], r;
r = reviewGuess (z,y);//calling the function that determines the results
switch (r)//switch statement for function results, letting the user know if they matched the number, if the number is higher, or lower
{
case 0:
cout << "You Win!!" << endl;
cout << "\n";
cin.get();
return;
case 1:
cout << "The number is higher than your guess" << endl;
break;
case -1:
cout << "The number is lower than your guess" <<endl;
break;
}
if (count == 15)
{
cout << "Sorry, no guesses remain. The random number was... " << RandomNumber << "!";//so the user can see the random number at the end of their attempts
cout << "\n";
cin.get();
Again();
}
}
return;
}
int DisplayGuess(int member[])
{
for(int i = 0; i < 15; ++i)
cout << "\nGuess " << i + 1 << ": " << member[i];
cout << endl;
return;
}
Try this inside your loop
if(count > 0)
{
for (int j= 0; j < count; j++)
{
cout<<Guess[j];
}
}
Call DisplayGuess() in the first line of the for loop. Since the first you time you call it your array is empty, it shouldn't output anything.
So,
for (int count = 0; count < attempts; count++)
{
DisplayGuess(Guess[count]);
cin >> Guess[count];
int z = RandomNumber, y = Guess[count], r;
r = reviewGuess (z,y);//calling the function that determines the
results
. . . . . .