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 have the following code. I am trying to populate an array with a deck of cards, and I keep encountering the same error "expected expression error" no matter how I code the loops to populate the array.
Can anybody see where I'm going wrong. I think its something painfully simple, that I, who am new to C++, am just missing.
Thanks!!
#include <iostream>
using namespace std;
struct playingCard{
char suit; // heart (1), club (2), spade (3), diamond (4)
int value; // 1 to 13 (ace is LOW)
};
void printArray(playingCard playingCardArray[], int size){
for (int i = 0; i < size; i ++){
cout << playingCardArray[i].suit << ":\t" << playingCardArray[i].value << endl;
}
}
int main()
{
const int ARRAY_SIZE = 52;
playingCard playingCardArray[ARRAY_SIZE];
int i = 1;
int suitLoop = 1;
while (suitLoop == 1){
for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
playingCardArray[i] = {suitLoop, valueLoop},
}
}
printArray(playingCardArray, ARRAY_SIZE);
return 0;
}
To resolve your compilation issue change you inner for loop like this:
for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
playingCardArray[i].suit = suitLoop;
playingCardArray[i].value = valueLoop;
}
Other than compilation your code also has Infinite loop , to resolve this you need to change your main somewhat like this:
int main()
{
const int ARRAY_SIZE = 52;
playingCard playingCardArray[ARRAY_SIZE];
int i = 1;
int suitLoop = 0;
while (suitLoop < ARRAY_SIZE){
for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
playingCardArray[suitLoop].suit = (suitLoop/13 + 1);
playingCardArray[suitLoop++].value = valueLoop;
}
}
printArray(playingCardArray, ARRAY_SIZE);
return 0;
}
Exchanging the comma with a semicolon at the end of playingCardArray[i] = {suitLoop, valueLoop}, solves the problem.
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 11 hours ago.
Improve this question
This is my problem. My task is to code the function void:
void process(char input[], char output[]){
}
And the given:
int main(){
const int MAX_SIZE = 100;
char input[] = "this is a beautiful day" ;
char output[MAX_SIZE];
process(input, output);
cout << output;
return 0;
}
Note: I just can use and for this question.
This is my work:
void process(char input[], char output[]){
int n = strlen(input);
for (int i = 0; i > n; i++){
output[i] = input[i];
input[i] = input[n - i + 1];
input[n - i + 1] = output[i];
}
}
But it doesn't work. How should I fix it?
there's a logical error with your code.
for (int i = 0; i > n; i++){
output[i] = input[i];
input[i] = input[n - i + 1];
input[n - i + 1] = output[i];
In your looping statement, you've initialised i=0 and given the condition i>n. This will result in the loop statement not executing.
Secondly, I have simplified your code further, and it should provide the required input.
for (int i = 0; i<=n; i++){
output[i] = input[n - i]; //1
cout<<output[i];
}
So essentially, a string is an array of characters, so to simplify, since the index in the array output is running from 0 to n, simultaneously, the array input indexes are being accessed from n to 0, in the line //1 .
As mentioned, since a string is an array of characters, within the same loop, i'm displaying each element in the output string. Providing the answer.
My test case was : This is a test
Output: tset a si sihT
Hope this helps! Let me know if you need any further explanation.
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
I'm trying to write a c++ function to find if the array has capital, but somehow it kept returning true.
Here is my function:
bool hasNoCapitals (const string array[], int n)
{
bool result = true;
for (int i = 0; i<0; i++)
{
for (size_t k = 0; k < array[i].size(); k++)
{
char word = array[i][k];
if (word == tolower(word))
{
result = true;
}
else{
result = false;
k = array[i].size();
return result;
}
}
}
return result;
}
int main()
{
string arr[] = {"sa","DDDD","DDDDDDDDD","ffdd","sa"};
cout << hasNoCapitals(arr, 5);
return 0;
}
The function always returns true because of a typo: you have for (int i = 0; i<0; i++), which should be for (int i = 0; i<n; i++) instead.
So your loop is always empty.
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
I just recently learned to do selection sorting an array. I am also using X-Code on a mac.
I thought I did everything correctly, but I seem to keep getting this error message on the if statement :
Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff5fc00000).
What am I doing wrong?
using namespace std;
void selectionSorting(int array[], int n)
{
for(int i = 0; i < n-1; n++)
{
int min = i;
for(int j = i + 1; j < n; j++)
{
if(array[j] < array[min]) //Thread 1: EXC_BAD_ACCESS(code=1, address=0x7fff5fc00000)
min = j;
}
int temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
int main()
{
int n = 10;
int array[]= {10,9,8,7,6,5,4,3,2,1};
selectionSorting(array, n);
for(int x=0; x < n; x++)
{
cout << array[x] << " ";
}
return 0;
}
You have a logical error at for(int i = 0; i < n-1; n++). It should be for(int i = 0; i < n-1; i++) (iterate through the elements of the array).
Also EXC_BAD_ACCESS suggests that your are trying to access a piece of memory that is no longer accessible or it doesn't go well with the intended use.
See that this occurs at if(array[j] < array[min]), which is obvious because j is going beyond the array length as you do n++.
As suggested in the comments try using a debugger.
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 7 years ago.
Improve this question
I have encountered a problem with printing 2D arrays. Here is my code, any help will be greatly appreciated.
#include<iostream>
using namespace std;
int main()
{
int NumRow, NumColumn;
int anArray[2][2] = {{1,2},{3,4}};
for (int N_column = 1; N_column < NumColumn; N_column++)
{
for (int N_row = 1; N_row < NumRow; N_row++)
{
cout << anArray[N_row,N_column];
}
}
return 0;
}
3 problems:
Array indexes start at 0.
NumColumn, NumRow are uninitialized.
wrong syntax [y,j], use [i][j]
Try like this:
...
int NumRow = 2, NumColumn = 2;
int anArray[2][2] = {{1,2},{3,4}};
for (int N_column = 0; N_column < NumColumn; N_column++)
{
for (int N_row = 0; N_row < NumRow; N_row++)
{
cout << anArray[N_row][N_column];
}
}
...
You declare
int NumRow, NumColumn;
but you never assign a value to them. Use
int NumRow = 2, NumColumn = 2;
instead. Also, C-arrays start at 0, not at 1, so you must update your for-loops as well:
for (int N_column = 0; ...
for (int N_row = 0; ...
Last, change the output statement, as multidimensional arrays need to be reached in a different way:
cout << anArray[N_row][N_column];
There are few issues in your code:
1st: You declare NumRow, NumColumn but use them without initializing them before which cause to Undefined Behaviour.
Solution: Initialized them
NumRow = 2;
NumColumn = 2;
2nd: Array syntax in the following line-
cout << anArray[N_row,N_column];
it should be
cout << anArray[N_row][N_column];
3rd: C++ arrays are zero indexed, so you should start initializing the loop control variables like following:
for (int N_column = 0; N_column < NumColumn; N_column++)
{ ^^^
for (int N_row = 0; N_row < NumRow; N_row++)
{ ^^^^
//...
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 couldn't figure out why but this is my code :
char expression[256] = {};
cin >> expression;
cout << endl;
int** variableTable = NULL;
int numOfVals = getNumberOfVariables(expression);
variableTable = static_cast<int**>(calloc(numOfVals, sizeof(int*)));
for (int i = 0; i < numOfVals; i++)
{
variableTable[i] = static_cast<int*>(calloc(2, sizeof(int)));
}
fillPromoterTable(expression, variableTable, numOfVals);
this is the fillPromoterTable
void fillPromoterTable(const char* expression, int** variableTable, int numOfVals)
{
char promoter[15] = {};
char *token;
char* expCpy = pcstrdup(expression);
for (int i = 0; numOfVals; i++)
{
token = strtok(expCpy, "+-*/");
int nLen = istrlen(token);
for (int j = 0; j < nLen; j++)
{
if (isdigit(token[j]))
promoter[j] = token[j];
if (isalpha(token[j]))
break;
}
variableTable[i][0] = atoi(promoter);
memset(promoter, '\0', 15);
token = strtok(NULL, "+-*/");
}
free(token);
free(expCpy);
}
in this line :
variableTable[i][0] = atoi(promoter);
I get an error saying I'm trying to write to address 0xFDFDFDFD
I couldn't figure out why it happens I could use some help.
Change the for loop condition in the function fillPromoterTable
for (int i = 0; numOfVals; i++)
To:
for (int i = 0; i < numOfVals; i++)