I'm new to C++ and using minGW version 6.3.0-1. I am not able to compile this code.
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int r, c, a[5][5];
cout << "Test loop";
for (int x = 1; x <= 6; ++x)
{
cout << "Value of variable x is: " << x << endl;
}
cout << "Test loop ends" << endl;
cout << "Enter the number of rows and columns:";
cin >> r >> c;
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
cout << "Enter the array element:";
cin >> a[i][j];
}
}
cout << "The array you entered:" << "\n order:" << r << "x" << c;
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
cout << a[i][j] << " ";
}
cout << endl;
}
return 0;
getch();
}
Also please help find out which C++ standard im using currently.
return 0;
getch();
}
Your main() returns before waiting for a character using getch(). Switch those two lines:
getch();
return 0;
}
Also please help find out which C++ standard im using currently.
If you don't specify -std when you compile your code, gcc 6.3 defaults to -std=gnu++14 which means you are using C++14 with GNU extensions. See the documentation for further details.
Related
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
I need my code to output the common numbers from two arrays but I can't seem to find the issues in my code. Here are some more words so I can fill the quota for stack's post requirement.
Here's the code.
#include <iostream>
using namespace std;
int main()
{
//Variables
int list1[5], list2[5];
cout << "Enter numbers for List No.1" << endl;
for (int i = 1; i <= 5; i++) {
cout << "Enter number " << i << ":";
cin >> list1[i];
}
cout << "Enter numbers for List No.2" << endl;
for (int i = 1; i <= 5; i++) {
cout << "Enter number " << i << ":";
cin >> list2[i];
}
{
cout << "common elements" << endl;
for (int i = 0; list1; i < i++) {
for (int i = 0; list2; i < i++)
if (list1[i] == list2[i]) {
cout << " " << list1[i];
}
}
}
return 0;
}
You should use different counter for each loop and also your loop has wrong parameters. Do it like:
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (list1[i] == list2[j]){
cout<<" "<<list1[i];
}
}
}
Also for initial values start from zero index([0]) as zero index is the first element for each array:
for (int i = 0; i < 5; i++) {
cout << "Enter number " << i << ":";
cin >> list1[i];
}
Another way to do this is by using std::array. It acts very similar to a normal array but it is much better to work with.
for example if you want a char array of length of 10 you can simply write:
std::array<char, 10> My_happy_array.
First please read this to understand why you should not using namespace std. Second consider reading about Range-based for loop
Third, here is the modified version of your program which uses std::array instead of a normal array:
#include <iostream>
#include <array>
int main() {
std::array<int,5> list1;
std::array<int,5> list2;
std::cout << "Enter numbers for List No.1" << std::endl;
for (int i = 0; i < list1.size(); ++i) {
std::cout << "Enter number " << (i+1) << ":";
std::cin >> list1[i];
}
std::cout << "Enter numbers for List No.2" << std::endl;
for (int i = 0; i < list2.size(); ++i) {
std::cout << "Enter number " << (i+1) << ":";
std::cin >> list2[i];
}
for(auto i : list1){
for(auto j : list2){
if(i==j){
std::cout << " " << i;
}
}
}
return 0;
}
Here's fixed code
#include <iostream>
using namespace std;
int main()
{
//Variables
int list1[5] ,list2[5];
cout << "Enter numbers for List No.1" << endl;
for(int i = 0; i < 5; i ++)
{
cout << "Enter number " << i << ":";
cin >> list1[i];
}
cout << "Enter numbers for List No.2" << endl;
for(int i = 0; i < 5; i ++)
{
cout << "Enter number " << i << ":";
cin >> list2[i];
}
cout<<"common elements"<<endl;
for (int i = 0;i<5; i++)
{
for (int j = 0;j<5; j++)
{
if (list1[i] == list2[j])
{
cout<<" "<<list1[i];
}
}
}
return 0;
}
mistakes in code:
you used for loops wrongly whole the time.
array of 5, stores values in index (0,1,2,3,4) not (1,2,3,4,5), so always start loop from 0 and ends before max index (for this case '4' ie. use 'i<5')
while using for loop inside other for loop never use same iterable variable like you used i in both loops use i and j different for each loop,
take a look in my code..
ask me if you get any difficulties.
I'm currently self teaching C++, and have a problem. the purpose of this code is to print asterisks in a pyramiding fashion, with the example being if the input (variable int n) is 5, it should print like this:
*
**
***
****
*****
Here's the code:
#include <iostream>
using namespace std;
int main()
{
int i, n;
cout << "What is your number?" << endl;
cin >> n;
cout << "n is: " << n << endl;
int arr[n];
int z = n;
while(z > 0){
arr[z] = 0;
z--;
}
z = n;
for(int y = 0; y<=z; y++){
arr[y] = z;
cout << "Y is: " << y << endl;
cout << "Arr[Y] is: " << arr[y] << endl;
cout << "z is: " << z << endl;
z--;
}
while(n > 0){
int x = arr[n];
while(x > 0){
cout << "*";
x--;
}
cout << endl;
n--;
}
}
but the first half (rounded up) will always be blank on printing. I don't know how to debug in CodeBlocks yet, so I can't tell you what's hiding in the memory to solve this myself
Although this does not fix your code, you stated that you are currently learning C++, so I think providing a better solution is even much better.
Here is another simpler way to achieve your goal:
#include <iostream>
using namespace std;
int main()
{
unsigned int i, n;
cout << "What is your number?" << endl;
cin >> n;
for (auto i = 0; i < n; i++)
{
for (auto j = 0; j <= i; j++)
{
cout << "*";
}
cout << endl;
}
}
Remember, trying to implement an easy, simple and efficient way is always better for performance, readability and debugging. Good luck in your learning and happy coding!
The most easiest way to do this
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cout << string(i, '*') << endl;
}
return 0;
}
I want to find the factors of a product by using arrays to check whether they equal it or not
the values do not print
here is the code
#include <iostream>
using namespace std;
int main() {
int arr[5] = { 1,3,5,7,2 };
int arr1[5] = { 0,6,5,4,9 };
int X;
cout << "Please enter X:"; cin >> X;
for (int i = 0, j = 0; i < 5 && j < 5; ++i, ++j) {
if (arr[i]*arr[j]==X) {
cout << arr[i] << " ";
cout << arr1[j] << " ";
}
}
}
Use this nested loops
for (int i = 0; i < 5 ;++i) {
for(int j=0 ;j<5;++j){
if (arr[i]*arr1[j]==X) {
cout << arr[i] << " ";
cout << arr1[j] << " ";
}
}
}
This question already has answers here:
How to make cin take only numbers
(2 answers)
Closed 6 years ago.
So the requirements for this program is to be able to increment arrays of the same size (size from 5 to 15 indexes) and increment each element in the array by one using for and while loops. The last task is to take values from the first array and put them in reverse order and assign them to the second array.
So everything works as normal, and the program rejects invalid inputs and does not go into an infinite loop. However, the program accepts some inputs that are not wanted.
For example, I would input something like '12 a' or '7 asdfkla;j lasnfg jasklgn asfg' and it would go through. It is interesting too because the code registers only 12 or 7 and completely ignores the rest. I think it is because once it hits a non-integer character, it would stop ignore the rest.
Why is it ignoring the rest of the input? And is there a way to catch this error from going through?
Also, if you see anything that catches your eye, feel free to critique c: I am always looking to improving.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL));
int x;
int j = 0;
bool not_valid = true;
system("color f");
cout << "Program will ask for an input for the size of an array.\n"
<< "With the array size defined, program will generate semi-\n"
<< "true random integers from 0 to 8. First array will then\n"
<< "be assigned to the second in reverse (descending) order.\n\n";
do {
cout << "Enter array size (0 - 15): ";
cin >> x;
if (x >= 5 && x <= 15) {
not_valid = false;
cout << "\nArray size: " << x << endl;
}
else {
cout << "Invalid input.\n\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
} while (not_valid);
int *arr0;
int *arr1;
arr0 = new int[x];
arr1 = new int[x];
for (int i = 0; i < x; i++) {
arr0[i] = rand() % 9;
}
for (int i = 0; i < x; i++) {
arr1[i] = rand() % 9;
}
cout << "\nARRAY 0 (unmodified, for):\n";
for (int i = 0; i < x; i++) {
cout << arr0[i] << "\t";
}
cout << "\n\nARRAY 0 (modified, for):\n";
for (int i = 0; i < x; i++) {
arr0[i]++;
cout << arr0[i] << "\t";
}
cout << "\n\nARRAY 1 (unmodified, while):\n";
for (int i = 0; i < x; i++) {
cout << arr1[i] << "\t";
}
cout << "\n\nARRAY 1 (modified, while):\n";
while (j < x) {
arr1[j]++;
cout << arr1[j] << "\t";
j++;
}
int second = x - 1;
for (int i = 0; i < x; i++) {
arr1[second] = arr0[i];
second--;
}
j = 0;
cout << "\n\nARRAY 1 (array 0, descending):\n";
while (j < x) {
cout << arr1[j] << "\t";
j++;
}
cout << endl << endl;
system("pause");
return 0;
}
Take input in string and then check if it's a number or not.
Example:
#include<iostream>
#include<sstream>
#include <string>
using namespace std;
int main()
{
string line;
int n;
bool flag=true;
do
{
cout << "Input: ";
getline(cin, line);
stringstream ss(line);
if (ss >> n)
{
if (ss.eof())
{
flag = false;
}
else
{
cout << "Invalid Input." << endl;
}
}
}while (flag);
cout << "Yo did it !";
}
I need help understanding the difference in logic between the while loop / for loop here's the sample code :
#include<iostream>
using namespace std;
int main(void)
{
cout << "A multiplication table:" << endl
<< " 1\t2\t3\t4\t5\t6\t7\t8\t9" << endl
<< "" << endl;
for(int c = 1; c < 10; c++)
{
cout << c << "| ";
for(int i = 1; i < 10; i++)
{
cout << i * c << '\t';
}
cout << endl;
}
return 0;
}
I tried rewriting it as a while loop , but the outcome is missing information.
#include <iostream>
using namespace std;
int main() {
int i = 1;
int c = 1;
while (c< 10){
cout << c <<"|";
c++;
while (i< 10){
cout << i * c << '\t';
i++;
}
cout << endl;
}
cin.clear();
cin.ignore();
cin.get();
return 0;
}
Someone suggested that resetting i to 1 would give the rest of the results, i'm having trouble understanding why the while loop requires a reset whereas the for loop does not.
You would have to set i=1 to get your expected behavior in both examples. With the for loop, this is already taken care of since in the header of the for loop, there is a for(int i = 1; ...; ...).
for (i=0;i<n;i++) {
dosomething;
}
is equivalent to:
i=0;
while (i<n) {
dosomething;
i++;
}
The problem in your code is that you don't reset i to 1 in the inner loop.
Declare int i=1 inside the loop over c, not outside of it.
Try this:
#include <iostream>
using namespace std;
int main() {
int c = 1;
while (c< 10){
cout << c <<"|";
c++;
int i=1;
while (i< 10){
cout << i * c << '\t';
i++;
}
cout << endl;
}
cin.clear();
cin.ignore();
cin.get();
return 0;
}