Help calling a void function.

Just need a little help for this one. I'm beginning to learn how to use functions, and I need for this one to output a straight line based on how long the user wants it, and what fill character they want. How can I do that? I tried a few different ways to get the line to output but it hasn't worked yet
Here's my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

void line(int width, char fill_char){
  for(int a = 1; a <= width; a = a + 1){
    cout << " " << fill_char << endl;
  }
}

int main(){

  int width;
  int shape;
  int fill_char;
  
  cout << "Welcome to Picture Maker!\nWhich shape should I draw?: ";
  
  cin >> shape;
  if (shape == 1){
    cout << "\nFill character?: ";
    cin >> fill_char;
    cout << "\nHow wide?: ";
    cin >> width;
    // Call the function here
  }
}
Last edited on
Exactly what do you need help with? If you want to call the function you simply write

line(width, fill_char);
Last edited on
My code keeps terminating now right after I ask for the width..? I have no idea why it's doing this. It terminates right before the user gets to enter the width.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

using namespace std;

void line(int width, char fill_char){
  for(int a = 1; a <= width; a = a + 1){
    cout << " " << fill_char << endl;
  }
}

int main(){

  int width;
  int shape;
  int fill_char;
  
  cout << "Welcome to Picture Maker!\nWhich shape should I draw?: ";
  
  cin >> shape;
  if (shape == 1){
    cout << "Fill character?: ";
    cin >> fill_char;
    cout << "How wide?: ";
    cin >> width;
    line(width, fill_char);
  }
}
Last edited on
int fill_char;
 
void line(int width, char fill_char){


is fill_char an int or a char? Pick one.
Oh, that last one worked. It seems to be working fine now. Thanks guys
Topic archived. No new replies allowed.