This topic is 3775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

For some reason, when I put the statement "int j = 0;" along with the other variable declarations at the top of the program (e.g, "int rows, columns;" etc), it won't work properly. It only works right if I keep the statement where it is now. Could someone please explain why this is?? As for the output, it is supposed to be a matrix. Therfore, if the vaules were, rows = 4 columns = 6 theChar = A the output would look like this: AAAAAA AAAAAA AAAAAA AAAAAA -------------------------------------------------------------------------------- #include <iostream> int main() { int rows, columns; int i = 0; char theChar; std::cout << "How many rows? "; std::cin >> rows; std::cout << "How many columns? "; std::cin >> columns; std::cout << "What character? "; std::cin >> theChar; while (i < rows) { i++; int j = 0; while (j < columns) { j++; std::cout << theChar; } std::cout << "\n"; } return 0; } -------------------------------------------------------------------------------- Any help or advice will be very much appreciated!

Share on other sites
If you have int j = 0; in the top you should remember to set j = 0 each time you change rows.

And I would probably use for loops instead of while loops for what you are trying to do[smile].

Share on other sites
you could put:

int j=0;

at the beginning of the program

but then you need to have another : j = 0; inside the loop (where it is now)

try to follow the program logic, you need to set j back to zero after each iteration.

here is the code:

#include <iostream>int main(){int rows, columns;int i = 0;int j = 0;char theChar;std::cout << "How many rows? ";std::cin >> rows;std::cout << "How many columns? ";std::cin >> columns;std::cout << "What character? ";std::cin >> theChar;while (i < rows){i++;j = 0;while (j < columns){j++;std::cout << theChar;}std::cout << "\n";}return 0;}

[Edited by - memento_mori on August 15, 2007 4:19:25 PM]

Share on other sites
1. For Beginners.
2. Initialization is not the same thing as assignment.
3. For a *counted* number of iterations, use a for loop instead of a while loop.

Share on other sites
std::cin>>variable; is very bad. If I put 'a' into that line, I'll ruin your day. You need to check for the validity of input. If you get bad input, you can either throw an exception, or just demand better input. Below I use this second route - it works very well for such a simple application.

Speaking of bad input, you should use safter practices. I prefer scanf and printf for such a simple application. Essentially they are the same, but printf() is good to get used to, especially if you want to print multiple variables in a single without having to constantly use <<.
printf("%s, you are %d years old, and born on %d/%d in the year %d\n",name,age,day,month,year);
as opposed to
std::cout<<name<<" you are "<<age<<" years old, and born on "<<day<<"/"<<month<<" in the year "<<year<<endl;
In addition, if you do want to do std::cin, it's easier to just add the line[code]using namespace std;[/quote]after your includes, then you don't need the std:: part.

Like Zahlman said, a for loop is better than your while construction, and if those variables are only ever going to be used once, then you can nest the declaration in the loop itself.

Here's an example of what I said.
#include <iostream>#include <cstdio>#include <cstdlib>using namespace std;//name::function is a function stored in a "namespace"int main(int argc,char**argv[]){	char columns[255],rows[255],character[255];	int rowcount=0;	int columncount=0;	while(rowcount<1){//get a valid input!		cout<<"How many rows?  ";//no std:: needed anymore		scanf("%s",rows);		rowcount=atoi(rows);	}	while(columncount<1){		printf("\nHow many columns?  ");		scanf("%s",columns);		columncount=atoi(columns);	}	printf("\nAnd what is the character?  ");	scanf("%s",character);	printf("\n");	for(int i=0;i<rowcount;i++){		for(int j=0;j<columncount;j++)			printf("%s",character);		printf("\n");	}	return(0);}

Share on other sites
Very informative. Thank you all so much. The way I have interpreted the solution, is that "int j = 0;" has to be included in the "while" statement, because you can't assign the value "0" to "int j" and keep it as "0" unless its inside the while statement. Otherwise the program won't reset "int j" to "0" as it iterates.

Share on other sites

This topic is 3775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• Forum Statistics

• Total Topics
628721
• Total Posts
2984394

• 25
• 11
• 10
• 16
• 14