10 Replies - 661 Views - Last Post: 10 April 2012 - 03:03 PM Rate Topic: -----

#1 barkinbilgi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 09-April 12

C++, making Square with using Functions.. please somebody help..

Posted 09 April 2012 - 08:33 PM

#include <iostream>
using namespace std;
int getInput(void){
int s;
do{
    ;
    cout<<" Enter length between 0 and 64 (-1 to exit): "<<endl;
    cin>>s;
    cout<<" Length Entered: "<<s<<endl;
}
while( (s>-1)||(s<64));
    return s;
}
void pritline(char A,char B,int n){
for(int i=1;i<=n;i++)
if(i==1||i==n)cout<<A;
else
cout<<B;
}
void printStats(int N,int s){
cout<<N<<"squares are printed. Their average="<<(s/N)<<endl;
}
int main(){
    int sum=0, N=0;
    while(1)
    {
    int p=getInput();
    if(p=-1)break;
    sum+=p; N++;
    for(int i=1;i<=p;i++)
    if(i==1||i==p)
    void printline('+','+',p);
    else 
    void printline('+','-',p);
    }
    void printStats(int N,int s);
    
    system("pause");
    return 0;
    }




Here is my assingment, please somebody help me, I am so confused., why doesnt work

Re-write your Squares program so that it uses the following functions.
int getInput(void); //function prototype

This function prompts the user to enter an integer. After extracting data from cin, if cin.fail() is true, then print an error message and return -1. If a negative integer is entered, then print an error message and re-prompt the user; otherwise, return the integer.

If getInput() returns a -1, then the calling function should print the number of squares printed along with their average length using the printStats() function and terminate the program.

void printLine(char, char, int); //function prototype

This function prints a line for the square. The first char parameter is the end character. The second char parameter is the interior character. The third parameter is the line length.

Example usage.

int length = ...; //variable storing the square length

// print top line
printLine('+', '-', length);

// print interior line
printLine('|', ' ', length);

void printStats(int, int); //function prototype

This functions prints the number of squares printed and their average length. The first int parameter is the number of squares printed. The second int parameter is the total of the printed square lengths. This function prints a line having the following format.

X squares printed. Average length: Y.YY
Also it was my square assignment, but I couldnt do with in functions//



#include <iostream>
using namespace std;
int main(void){

int s,n;
do{
cout << "Enter length between 0 and 64 (-1 to exit):" ;
cin >> s;
cout << "Length entered :"<< s <<endl;
if (s<-1 || s>64 ){
cout << s <<" is invalid" <<endl;
cout <<"Length must be between 0 and 64 inclusive, or enter -1 to exit\n";
continue;

}
if(s==-1) break;
if(s==0){
cout <<" " <<endl;
}
int p=1;
while( p<=s){
cout << ((p==1 || p==s)? '+':'|');
for( n=2 ;n<s;n++)
cout << ((p==1 || p==s)? '-':' ');
if(s>1)
cout << ((p==1 || p==s)? '+':'|');
cout <<endl;
p++;
}

}while(true);

system("pause");
return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: C++, making Square with using Functions.. please somebody help..

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,963
  • Joined: 19-February 09

Re: C++, making Square with using Functions.. please somebody help..

Posted 09 April 2012 - 09:19 PM

Hi, to compare values the comparison operator is used ==, not the assignment operator =

So this is wrong :

28	    if(p=-1) break;




In the getInput function you want to loop while the number is less than -1 or the number is greater than 64.

So this is incorrect :

11	while( (s>-1) || (s<64));




This function has been misspelled.

14	void pritline(char A,char B,int n){


Was This Post Helpful? 0
  • +
  • -

#3 barkinbilgi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 09-April 12

Re: C++, making Square with using Functions.. please somebody help..

Posted 09 April 2012 - 10:29 PM

I got these errorrs. I fix them but It is still not working.. says error in line 32,, and what do you mean this function has misspelled?
Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: C++, making Square with using Functions.. please somebody help..

Posted 09 April 2012 - 10:31 PM

Pritline. #define thought you meant PrintLine. And what is the error one line 32? All I see is a blank line.
Was This Post Helpful? 0
  • +
  • -

#5 barkinbilgi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 09-April 12

Re: C++, making Square with using Functions.. please somebody help..

Posted 09 April 2012 - 10:44 PM

I dont know how to use that expression #define :( compiler says 32
32-variable or field `printline'

32-initializer list being treated as
34-variable or field `printline'
34 initializer list being treated as

I gave 2 codez, one is contain function the other is in main function.
This function assingment is supposed to write squares like.
when you enter 3
+-+
| |
+-+ or when you enter 5

+---+
| |
| |
| |
+---+ I believe this code have lots of error.
Was This Post Helpful? 0
  • +
  • -

#6 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: C++, making Square with using Functions.. please somebody help..

Posted 09 April 2012 - 10:48 PM

Oh, I see (I think). Lines 32 and 34 you are writing void pritline, hence declaring another void for Pritline. Just get rid of the word void from both 32 and 34.
Was This Post Helpful? 0
  • +
  • -

#7 barkinbilgi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 09-April 12

Re: C++, making Square with using Functions.. please somebody help..

Posted 09 April 2012 - 10:51 PM

Ok, now there is no error, it works this time but IT doesnt show squares...I am gonna be crazy..
Was This Post Helpful? 0
  • +
  • -

#8 barkinbilgi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 09-April 12

Re: C++, making Square with using Functions.. please somebody help..

Posted 09 April 2012 - 11:10 PM

Do you have any idea about that? I can not exit getInput function.. always keep asking enter lenght.. It is supposed to draw a squares..
Was This Post Helpful? 0
  • +
  • -

#9 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: C++, making Square with using Functions.. please somebody help..

Posted 09 April 2012 - 11:30 PM

Try something like:
If s==-1
{
//exit loop
}

This won't work. It's not full code.
Was This Post Helpful? 0
  • +
  • -

#10 barkinbilgi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 09-April 12

Re: C++, making Square with using Functions.. please somebody help..

Posted 10 April 2012 - 05:26 AM

It is not just a wrong part.. Ican you spend a few time please onto the code, I couldnt find the error..code works but doesnt draw a square..
Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1431
  • View blog
  • Posts: 4,963
  • Joined: 19-February 09

Re: C++, making Square with using Functions.. please somebody help..

Posted 10 April 2012 - 03:03 PM

In your getInput function you declare the s variable twice. The cin reads the number into the s variable in the inner scope. The while expression uses the s variable in the outer scope, so the while expression value doesn't change.


int getInput(void)
{
  int s;

  cout << endl;

  do 
  {
    // shadow variable
    // int s;
    cout << " Enter length between 0 and 64 (-1 to exit) : ";
    cin  >> s;
    cout << " Length Entered : " << s << endl;

  } while( (s<-1)||(s>64));

  cout << endl;
  return s;
}




What is the name of the function that prints a line? Here I changed it to printline.

//void pritline(char A,char B,int n)
void printline(char A,char B,int n)
{
  for(int i=1;i<=n;i++)
    if(i==1||i==n)
      cout << A;
    else
      cout << B;

  //cout << endl;
}





When you call the printline function in main, you pass the character 'p' as the third argument, when you want the length/size of the square.

int main()
{
  int sum=0, N=0;
  
  while(1)
  {
    int p = getInput();

    if(p==-1)
      break;
    
    sum += p; 
    N++;

    for(int i=1; i<=p; i++)
      if(i==1 || i==p)
         //printline('+', '+', 'p');
        printline('+', '+', p);
      else
        printline('+', '-', p);
  }

  //  this looks like a function prototype 
  //  and not a function call
  //  void printStats(int N, int s);

  system("pause");
  return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1