I want to draw and fill a rectangle using C++. The function parameter passed in must be a char, not an int. In my header file the drawing function is this:
void draw(char);
My rectangle.cpp file is this:
void rectangle::draw(char )
{
for(height=0;height<=height;height++)
{
for(width=1;width<=width;width++)
{
cout<<'*';
}
}
}
My main.cpp file is this:
rectangle d1;
d1.draw(char);
When I run the program it gives me the error:
Expected primary expression before 'char'.
I'm using Code::Blocks 13.12. Any ideas to solve this problem?
Your draw is missing a variable. Change draw(char); to draw(char c);
And both for loops need a maximum range, not the same value as the incrementing variable. height<=height; and width<=width;
#include <iostream>
using namespace std;
void draw(char c )
{
int rheight=10;
int rwidth=20;
for(int height=0;height<=rheight;height++)
{
for(int width=1;width<=rwidth;width++)
{
cout<<c;
}
cout<<"\n";
}
}
int main()
{
draw('*');
cout<<"\n";
return 0;
}
Related
I'm a newbie at C++, but I tried to do my research.However I seem to be not able to figure out what is wrong in my code, probably not familiar with the syntax, that I can't find. Trying to do something bigger, just testing some theory.
Here I have filled a 2D array, put in a function Func(). Lets suppose I did something to it, then I want to retrieve it from there. I imagined I could extract an address to the first array of arrays, but I keep getting the same error.
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <typeinfo>
using namespace std;
char(*)[15] Func(char A[15][15]) //Here is the problem
{
A[0][0]='O';
return A; //trying to return an address to the A[0]-1D array
}
int main() //This part isn't really important, just filling an 2-D array
{
char A[16][16];
int x,y;
for (y=0; y<=15;y++)
{
for(x=0;x<=15;x++)
{
if (3<=x<=12 and 3<=y<=12)
{
A[y][x]='*';
}
if (x==0 or x==15 or y==0 or y==15)
{
A[y][x]='#';
}
if ((x==2 and 2<=y<=13) or (x==13 and 2<=y<=13) or (y==2 and 2<=x<=13) or(y==13 and 2<=x<=13))
{
A[y][x]='#';
}
if (x==14 or y==14 or (x==1 and y==1))
{
A[y][x]='#';
}
}
}
for (int i=0,y=3, x=1; y<=12; y++, i++ )
{
char j='0'+i;
A[y][x]=j;
}
for (int i=0,y=1, x=3; x<=12; x++, i++)
{
char j='0'+i;
A[y][x]=j;
}
for (y=0; y<=15;y++)
{
for(x=0;x<=15;x++){
cout<<A[y][x];
}
cout<<endl;
}
char(*p)[15]=Func(A[15][15]); //here Im trying to assign pointer p to point to the first 1D array of 15x15 Array of A (To do pointer arithmetics later)
cout<<"p="<<p; //Just testing
return 0;
}
And I get this error:
expected unqualified-id before ')' token
expected initializer before 'Func'
Both directed at Func() function declaration line
Plus this error:
'Func' was not declared in this scope
Which is not pleasant too but less engaging than the previous ones, in my opinion.
I think the problem might be in the type of "A" (the pointer) or there is something I missed.
How can I fix the issues of this code? It feels like I have tried everything! Thank you.
You could use std::array instead. I also fixed the problems mentioned in the comments:
#include <array>
#include <iostream>
using namespace std;
using Field = std::array<std::array<char, 16>, 16>;
Field Func(Field A) //Here is the problem
{
A[0][0]='O';
return A; //trying to return an address to the A[0]-1D array
}
int main() //This part isn't really important, just filling an 2-D array
{
Field A;
int x,y;
for (y=0; y<=15;y++)
{
for(x=0;x<=15;x++)
{
if (3<=x and x<=12 and 3<=y and y<=12)
{
A[y][x]='*';
}
if (x==0 or x==15 or y==0 or y==15)
{
A[y][x]='#';
}
if ((x==2 and 2<=y and y<=13) or (x==13 and 2<=y and y<=13) or (y==2 and 2<=x and x<=13) or(y==13 and 2<=x and x<=13))
{
A[y][x]='#';
}
if (x==14 or y==14 or (x==1 and y==1))
{
A[y][x]='#';
}
}
}
for (int i=0,y=3, x=1; y<=12; y++, i++ )
{
char j='0'+i;
A[y][x]=j;
}
for (int i=0,y=1, x=3; x<=12; x++, i++)
{
char j='0'+i;
A[y][x]=j;
}
for (y=0; y<=15;y++)
{
for(x=0;x<=15;x++){
cout<<A[y][x];
}
cout<<endl;
}
auto p = Func(A); //here Im trying to assign pointer p to point to the first 1D array of 15x15 Array of A (To do pointer arithmetics later)
cout<<"p=\n";
for (const auto &row : p) {
for (const auto &el : row)
cout << el; //Just testing
cout << '\n';
}
return 0;
}
You need C's "declaration follows use" principle.
You would access a single array element of the result as
(*Func(some_array))[x]
so the prototype would be
char (*Func(char A[15][15]))[15]
But it's slightly more readable to use a type alias instead.
For instance,
using FifteenChars = char[15];
FifteenChars* Func(FifteenChars A[15])
{
return A;
}
There are a few other issues:
There is a conflict between your array sizes.
A[15][15] is not the top left part of A but a single char, the one in the bottom right corner.
3<=x<=12 is not 3<=x && x<=12, but (3<=x)<=12, and 3<=x converted to int is either 0 or 1.
You need to spell it out, or split the loop into several individual parts.
#include <iostream>
#include <string.h>
using namespace std;
class sorting
{
private:
char str[10];
public:
sorting() {
int i;
for(i=0;i<10;i++) {
cin>>str[i];
}
}
void sort() {
int i,j;
char temp;
for(i=0;i<10;i++) {
for(j=i+1;j<10;j++) {
if(strcmp(str[j],str[j+1])>0) {
strcpy(temp,str[j]);
strcpy(str[j],str[j+1]);
strcpy(str[j+1],temp);
}
}
}
for(i=0;i<10;i++) {
cout<<str[i];
cout<<"\n";
}
}
};
int main() {
sorting s1;
cout<<s1.sort();
return 0;
}
This is a code I have written to sort strings using constructors. It gives me error in the if condition of the code where I have used strcmp. Please review this for I could not get the desired output and it gives me errors.
Problem 1
Like someone already pointed out, you cant use strcopy on chars. If you want to create a string array i would suggest using either char** or std::string*.
Problem 2
In your nested loop you will get an index out of bounds error, due to the fact that once i reaches a value of 8, j will be 9, which means that when you try to access str[j+1] which evaluates to str[10], you will get said error.
I am trying to get the output of this function in a .txt file named Password.txt. The function to print was running easily separately but as I put it inside this program to get the output, this error is showing:
Error: a function-definition is not allowed here before '{' token at line 6
I tried removing void but not working.
#include<iostream>
#include <fstream>
using namespace std;
void passn1()
{
void print(char set[],string pre,int n,int k)
{
if(k==0)
{
cout<<pre<<endl;
return;
}
for(int i=0;i<n;i++)
{
string newp;
newp=pre+set[i];
print(set,newp,n,k-1);
}
}
void printk(char set[],int k,int n)
{
print(set,"",n,k);
}
ptk()
{
char set1[]={'0','1','2','3','4','5','6','7','8','9'};
int k=6;
printk(set1,k,10);
}
}
int main()
{
ofstream fo;
fo.open("Password.txt",ios::out);
fo<<passn1();
fo<<endl;
fo.close();
return 0;
}
Please help me by telling where am I going wrong.
You are trying to define a function inside the body of another function, which is not allowed, as the compiler error suggests.
Moreover, you cannot send to std::ofstream a function call (fo<<passn1();), it doesn't make sense, since the function's return type is void (it returns nothing).
Since you have a recursive function (print()), the easiest thing would be to take the output stream to the file (std::ofstream) as a parameter in your function, and write pre directly to it. Of course you'd need to carry this ofstream parameter along the function chain.
Putting everything together, you'd something like this:
#include <iostream>
#include <fstream>
using namespace std;
void print(char set[], string pre, int n, int k, ofstream& fo)
{
if(k==0)
{
fo << pre << endl;
return;
}
for(int i=0;i<n;i++)
{
string newp;
newp=pre+set[i];
print(set, newp, n, k-1, fo);
}
}
void printk(char set[],int k,int n, ofstream& fo)
{
print(set, "", n, k, fo);
}
void ptk(ofstream& fo)
{
char set1[]={'0','1','2','3','4','5','6','7','8','9'};
int k=6;
printk(set1, k, 10, fo);
}
int main()
{
ofstream fo;
fo.open("Password.txt",ios::out);
ptk(fo);
fo<<endl; // this will append an empty line at the end of the file
fo.close();
return 0;
}
Output (content of Password.txt):
000000
000001
// rest of the data here...
999998
999999
I'm trying to create a two dimensional array, which I can use as coordinates for a map to be displayed. For the moment I'm just trying to get a character to display on screen over and over to create an effect in a box with the dimensions of the array. But at each coordinate it just displays some long number (maybe like the slots are empty or something?)
I feel like it might be a loss of data from the class member to the main function, but I'm really just guessing.
For example, I'm looking for an output something like this:
11111
11111
11111
11111
Source code:
#include <iostream>
using namespace std;
class Map_Blocks
{
public:
int Map_Width = 60;
int Map_Height = 15;
int Map_Array [15][60];
int Generate();
int Display();
};
int Map_Blocks::Generate()
{
int x, y;
for(y=0;y<Map_Height;y++)
{
for(x=0;y<Map_Width;x++)
{
Map_Array[y][x]=1;
}
}
return 0;
}
int Map_Blocks::Display()
{
int x, y;
for(y=0;y<Map_Height;y++)
{
for(x=0;y<Map_Width;x++)
{
cout<<Map_Array[y][x];
}
cout<<endl;
}
return 0;
}
int main(void)
{
Map_Blocks Size;
cout<<"Map Width ="<<Size.Map_Width<<endl;
cout<<"Map Height ="<<Size.Map_Height<<endl;
Map_Blocks disp;
disp.Display();
return 0;
}
You never call Generate() to initialize the array's content.
Add:
disp.Generate();
before the call to Display().
When I try to compile the following program it says "Build failed. Object reference not set to an instance of an object" . I'm kinda new to c++ so if anybody can help me it'll be great . I'm just trying out some example I saw in a book so I don't know whats wrong with this .
using namespace std;
class matrix
{
int m[3][3];
public:
void read(void);
void display(void);
friend matrix trans(matrix);
}
void matrix :: read(void)
{
cout<<"Enter the elements of the 3x3 array matrix : \n";
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"m["<<i<<"]["<<j<<"] =";
cin>>m[i][j];
}
}
}
void matrix :: display(void)
{
int i,j;
for(i=0;i<3;i++)
{
cout<<"\n";
for(j=0;j<3;j++)
{
cout<<m[i][j]<<"\t";
}
}
}
matrix trans(matrix m1)
{
matrix m2;
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
m2.m[i][j] = m1.m[j][i];
}
}
return(m2); //returning an object
}
int main()
{
matrix mat1,mat2;
mat1.read();
cout<<"\nYou entered the following matrix :";
mat1.display();
mat2 = trans(mat1);
cout<<"\nTransposed matrix :";
mat2.display();
getch();
return 0;
}
1 - Insert semi-colon after the class definition
2 - Insert the correct headers
#include <iostream>
#include <conio.h>
3 - Try getting a compiler that is a bit more descriptive with regards to errors. I did all that i mentioned and your program ran. Try it
It compiles fine after you fix your missing semi-colon (after your class declaration) and add either #include <conio.h> (Visual Studio) or #include <curses.h> (for a POSIX system) for the getch() function (which is not a standard function).