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 new to C++. I want to make a array of string in which I want to save names of players of a cricket team. I want to save this in the form of array like below:
player[1]= sachin;
player[2]= john smith;
I used program below but I am getting following error:
Error 1
error C2661: 'std::basic_istream>::getline' : no overloaded function takes 1 arguments
#include "stdafx.h"
#include <iostream>
#include<string>
int main()
{
using namespace std;
string player[3];
int i;
for (int i = 1; i <= 3; i++)
{
std::getline(cin, player);
}
}
How can I save string array like we do with numerical array.
Replace for (int i = 1; i <= 3; i++) with for (int i = 0; i <= 2;
i++)
getline() often causes errors and it wont simply work if you are using some flavor of Linux.
You are not indexing the array properly as in it should have been player[i] instead of just player.
if you are using using namespace std; then a better way of declaring is to declare it outside the main function.
Following code should do what you are trying to do.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
string player[3];
int i;
for (int i = 0; i < 3; i++) {
cin >> player [i];
}
}
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
#include <iostream>
#include <string.h>
using namespace std;
/* Variables */
int N = 0;
int M = 0;
int Px, Py;
int gcount = 0;
cin >> N >> M;
string maze[100];
/* Define Functions */
void read_maze(int);
void scan_maze(int);
void print_maze();
This is a code snippet for a text-based game I am making and when I run it, I get the following error:
main.cpp:10:1: error: 'cin' does not name a type
10 | cin >> N >> M;
| ^~~
I am not able to debug the problem with the code. Any ideas?
Editor: Atom
Operating System: Windows 10
Compiler: MinGW
Thanks
You must use a statement inside of a function, e.g.
#include <iostream>
int main() {
/* Variables are fine at global scope, but you should prefer local ones */
int N = 0;
int M = 0;
int Px, Py;
int gcount = 0;
/* Here's the problematic statement from before */
std::cin >> N >> M;
...
}
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 4 years ago.
Improve this question
In the following program:
#include <iostream>
#include <cmath>
using namespace std;
int diagonalDifference(int x[][],int n)
{
int sum1=0,sum2=0,y;
for(int i=0;i<n;i++)
{
sum1+=x[i][i];
sum2+=x[i][n-1-i];
}
y=abs(sum1-sum2);
return y;
}
int main()
{
int n,**z;
cin>>n;
int arr[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>arr[i][j];
}
}
z=diagonalDifference(arr,n);
cout<<x;
return 0;
}
I get a compilation error I don't understand.
error:declaration of 'x' as multidimensional array must have bounds for all dimensions except the first
Could you help me fix it?
int[][] is not a valid type:
int diagonalDifference(int x[][],int n)
You declare z as an int**:
int n,**z;
But you assign it an int:
int diagonalDifference(int x[][],int n);
z=diagonalDifference(arr,n);
And finally you print x which does not exist:
cout<<x;
As rules of thumb:
declare only one variable per line, and give it a meaningful name;
declare what possibly can as const;
Don't use C-style arrays unless you have to; prefer std::vector for instance;
don't use using namespace std;
much more you need to learn.
.
int diagonalDifference(int x**,int n) { /* .... */ }
int matrix_size = 0;
std::cin >> matrix_size;
std::vector<std::vector<int>> matrix{matrix_size, std::vector<int>{matrix_size}};
/* fill the matrix */
const int diag_diff = diagonalDifference(matrix, matrix_size);
std::cout << diag_diff << '\n';
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
I am trying to write a program which prints the cubes of inserted elements of array, and then prints the sum of elements divisible by 2, and not divisible by 3.
#include <iostream>
#include <math.h>
using namespace std;
void kub(int *niz[], int n)
{
int i=0;
while(i<n)
{
*niz[i]=pow(*niz[i],3);
i++;
}
}
void unesi(int* niz[],int n)
{
int i=0;
while(i<n)
{
cin>> *niz[i];
i++;
}
}
void stampaj(int* niz[],int n)
{
int i=0;
while(i<n)
{
if ((*niz[i]%2)==0 && (*niz[i]%2)!=0)
cout<<*niz[i]<<endl;
i++;
}
}
int main()
{
int n;
cin>>n;
int niz[n];
unesi(niz,n); /* <<= here */
kub(niz,n);
stampaj(niz,n);
return 0;
}
Everything is fine, until this line of code (marked "<<= here"): "unesi(niz,n);" What am I doing wrong?
Instead of:
void unesi(int* niz[],int n)
try:
void unesi(int niz[],int n)
niz[] decays to pointer, so no need to pointer to pointer
Same for the other functions. Last note, instead of cin>> *niz[i]; just use cin>> niz[i];
In C++,
int* nix[]
is actually translated into
int** nix
at compile time. To fix this you can use either
int* nix
int nix[]
Also in the function body you would probably need to change
*nix[i]
into
nix[i]
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 9 years ago.
Improve this question
I wanna get the sum of the first five natural numbers, but there in this this code something is wrong, need to find the misstake ? Help
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int i = 1, thesum;
while(i <= 5)
{
thesum += i;
i++;
}
cout << thesum;
return 0;
}
You haven't initialized thesum variable. Initialize it to 0.
int i = 1, thesum = 0;
Otherwise it will invoke undefined behavior.
As it was already pointed out you did not initialize local variable thesum. So it has some arbitrary value.
Also there is no any need to include header <cstdlib> because no one declaration from it is used.
As variable i is not used outside the loop it is better to make it a local variable of the loop.
So I would rewrite the program the following way
#include <iostream>
using namespace std;
int main()
{
const int N = 5;
int theSum = 0;
for ( int i = 0; i < N; i++ ) theSum += i + 1;
cout << "The sum of first " << N << " natural numbers is " << theSum << endl;
return 0;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have written down a code in C++ to make the words appear in array from a string input.
But it doesn't works due to overflow or something. Compiler doesn't show any error though.
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char a[100];
gets(a);
char b[100][100];
int I=0;
for(int j=0;j<strlen(a);j++) //runs the loop for words
{
int k=0;
do
{
b[I][k]=a[j];
k++;
}
while(a[j]!=' ');
I++;
}
cout<<b[0][0];
return 0;
}
If you're going to use C strings, you need to add a null terminator at the end of each string
do {
b[I][k]=a[j];
k++;
} while(j+k<strlen(a) && a[j+k]!=' ');
b[I][k] = '\0';
As ppeterka noted, you also need to change your loop exit condition to avoid an infinite loop.
Note also that the repeated calls to strlen here and in your code are wasteful. You should consider calculating this once before your for loop instead.
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char a[100];
gets(a);
char b[100][100];
int I=0;
int j =0;
while(j< 100) //runs the loop for words
{
int k=0;
do
{
b[I][k]=a[j+k];
k++;
} while(a[j+k]!=' ');
b[I][k+1] = '/0';
I++;
j= j+k;
}
cout<<b[0][0];
return 0;
}