Creating cpp multiplication table using for loop [closed] - c++

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 2 years ago.
Improve this question
output should be like this I hope u guys can help me..
Here's the code that I tried:
int main() {
int x,y;
for (x=1; x<=10; x++) {
cout << x << "|";
for (y=1; y<=10; y++) {
cout << x*y << "\t";
}
cout << endl;
return 0;
}
}

Your program is terminating just after the first iteration of the loop, the reason behind this is that you misplaced return 0 inside the loop instead of putting it at the end, so your program is returning after first iteration of the loop.
here is the fix
int main()
{
int x,y;
for (x=1; x<=10; x++)
{
cout<<x <<"|";
for (y=1; y<=10; y++)
{
cout<<x*y<<"\t";
}
cout<<endl;
}
return 0;
}

Related

"Exception thrown: Write access violation in C++" [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 2 days ago.
Improve this question
I get this error when I try to compile the following code: Exception thrown: write access violation.
this was 0xF81EFA0000.
#include <iostream>
using namespace std;
class Demo
{
int x;
public:
void setX(int i)
{
x = i;
}
int getX()
{
return x;
}
};
int main() {
Demo obj[4];
int i;
for (i = 0; 1 < 4; i++)
{
obj[i].setX(i);
}
for (i = 0; i < 4; i++)
{
cout << "obj[" << i << "].getX(): " <<
obj[i].getX() << endl;
}
// Textual Data Types
// Boolean
return 0;
}
The error concerns line 10, where "x = i" is written. Any help would be good.
I haven't tried anything to fix it, other than writing brackets after "x", which obviously didn't work. I expect this program to print an array of objects when working properly.

Why does my array only print once and not twice here? [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 last year.
Improve this question
int nums [] {1,2,3,4,5,6,7,8,9,10};
int main(){
int size {10};
for (int i;i<size;i++)
cout << nums[i] << " ";
for (int i;i<size;i++)
cout << nums[i] << " ";
return 0;
}
This prints my array once but shouldn't I get my array printed twice not once?
You need to initialize the i variable to the start index, like in for (i = 0; i < size; i++). Otherwise i can start in any value and this is undefined behavior.

Simple counting down code cant count down [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 1 year ago.
Improve this question
char x;
cout << "\n/something?(y/n): ";
cin >> x;
if(x=='y'){
int n2 = 0;
cout << "number: ";
cin >> n2;
int i = n2;
for(int i; 0<i; i--){
cout << i;
}
}
else{
system("pause");
}
How come when I run the code it doesn't count down from the number the user gave?
What is happening here is that you declare local variable i in the block which contains for loop, and then declare another local variable i in the loop itself, and the one in the loop is not initialized. It gets some "trash" value, from each it counts down. I assume you wanted to use i declated above the loop in the loop, but it is done differently. To fix this I suggest 2 most reasonable variants:
variant 1 - use i declared above the loop
int i = n2;
for(; i>0; i--) {
...
variant 2 - use loop-local i:
// just this, do not declare i above the loop
for(int i = n2; i>0; i--) {

Why does "std::cout << myVector[x][x] << std::endl" produce a subscript error? [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 8 years ago.
Improve this question
This is probably a very simple question, however it is hounding me.
I can get the data into the container but I can't then seem to print it to the console.
The following produces a subscript out of range debug error
Here is my code
std::vector<std::vector<int>> myVector;
for (int x = 0; x != 18; x++)
{
myVector.push_back(std::vector<int>(x,x));
std::cout << myVector[x][x] << std::endl;
}
Many thanks!
When you do std::vector<int>(x,x) inside for loop when x=0 you allocate zero elements. At other times you allocate x elements(row index:x and column indexes: 0,1,...x-1) and access x+1th element in xth row.
So change it to
for (int x = 0; x != 18; x++)
{
myVector.push_back(vector<int>(x+1,x));
std::cout << myVector[x][x] << std::endl;
}

I am learning C++ via a book. What I am doing wrong? [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 8 years ago.
Improve this question
I am learning C++ via a book. It told me to type this compile and link it in order for me to see if i can read it. But there are errors when I run this. What is the issue?
#include <iostream>
int main()
{
int x = 8;
int y = 6;
std::cout << std::end1;
std::cout << x - y << " " << x * y << " " << x + y;
std::cout << std::end1; return 0;
}
It should be std::endl; instead of std::end1;