# How can I multiply a char with an int? //School project

I was told to make a arduino function that prints * using serial
If I put 2 in the terminal then it prints **
if I put 5 in the terminal then it prints *****
I think did the basic function correct, but it won't print correct. it comes a number that I put and a rondom letter.
Is that possible that my way to multiply to type of input is wrong?

void setup(){
Serial.begin(9600);
}

void loop() {
int n = Serial.parseInt();
char k;
k = writeStars(n);
Serial.println(k);
}

char writeStars(int number){
Serial.print(number);
char s = '*';
char ans;
ans = s * number;
return ans;
}

No, what they want you to do is print something x number of times. That is (obviously!) different than printing x times something...

Have a look in the language reference at the for loop.

to explain a bit on the codelines:

``````char writeStars(int number)
``````

if the function with name "writeStars" is called for example

``````writeStars(7)
``````

this will do write a "7" on a piece of paper and put this piece of paper inside the box with name "number"

``````Serial.print(number);
``````

look inside the box with name "number" and read what is written on the piece of paper
In the example on the piece of paper is written a "7"
so Serial.print(number); will write a "7" on the serial monitor

``````char s = '*';
``````

create a box of type char (which is a kind of box that can only "hold" a single character. NOT more
put a piece of paper inside the box with a "*" written on the paper

``````char ans;
``````

create a box of type "char" . box stays empty

``````ans = s * number;
``````

take what is written on the paper stored in box with name "s"
and multiply with what is written on the piece of paper stored in the box named "number"

in the example this is a "7"
So you try to do a mathematical calculation "*" multiplied 7

Now please tell me what does a "*" represent for a mathematical number?
No number at all. So it does not work this way.

If you want to have success in your computer-lesson it is time to take time to really learn it.

From how you wrote your question you seem to be lazy. IMHO you have to make a decision

a: taking the laziness to the max and just copy & paste a working solution from another class-mate
hoping the teacher will not find out that you are cheating.

b: taking enough time to really learn how programming works

This will take multiple hours. For the most basic things it will take estimated 8 to 20 hours depending on your talent.

I recommend working through this tutorial. I t does not explain on the second page how your exercise can be solved.
It teaches you the basics how programming with the arduino-IDE works.

Learning to program

Work on until chapter 7.
Whenever a question arises you can ask the questions here. You can ask as many concrete questions as you like. As long as you show some own effort a lot of people here enjoy answering you.

best regards Stefan

That's right. Another way to say it, "7 times 10 equals 7777777777" is obviously not right.

The deadline is actually not very long. I know I did a lazy work this year, and I really regret.

But does it mean that I should use a for loop? But that was not told in the question. I am very confused about print something like this.

Yes this task is solved by using a for-loop. In the grown-up world nobody is telling you all the detail-steps how to finish a certain project. Your boss will expect that you know the details yourself.

So read in the reference how for-loops work. If you have questions post them here. As you yourself wrote the deadline is pretty soon you have to invest much time per day. If you go on in such small steps like just asking "do I need a for-loop?" I guess you are still too lazy.

Maybe it was not in the description because there are more ways than just a for-loop. You could use a while-loop as well and there are even more possabilities.

So give learning programming the highest priority.

Doing just this program is not very much. Read about for-loops and post your attempt (=your try to write the program) how it might work.

best regards Stefan

void setup(){

Serial.begin(9600);

}

void writeStars(int number){

for(int i = 1; i <= number; i++){

Serial.print(i * '*');

}

}

void loop(){

int number = Serial.parseInt();

writeStars(number);

while(1);

}

I got this worked with numbers only. but I still have no idea how to get that number of * I want.
Thank you a lot with the link you sent about the arduino course! Really helped me.
But I tried many methods, still failing
there's 6 question, and I was stuck on this one whole day.

You almost have it.

Let the for loop do the multiply. Just print the star.

You are reading from the serial buffer before you know that there is anything to read. That is what the available() function is for.

If you use the Serial.available() function you will not need the while(1).

I tried to put char i = '*'
Could you please explain more spesific?

Print the asterisk, in a for loop.
Don't assign it to another variable, don't try to do arithmetic on it.
Just print it, "number" times.

` for(int i = 1; i <= number; i++){` This isn't BASIC, and loops do not have to start from 1.

`````` for(int i = 0; i < number; i++){
``````

YES!!! I did it! Thank you guys for the help leading me to the goal pluss a lot of knowledge instead of giving me the ansver. Feels I learned alot!!

Just like Mr,AWOL said. print * under the for loop. But I still don't get it. How is this making any sense?? I didnt put anything front of the print expresstion in the for loop...

//Oppgave 3a
void setup(){
Serial.begin(9600);
}

void skrivStjerner(int antall){
for(int i = 1; i <= antall; i++){
Serial.print('*');
}
}

void loop(){
int antall = Serial.parseInt();
skrivStjerner(antall);
Serial.print('_');
}

You put a for loop in front of the print.
A for loop repeats.

Please remember to use code tags when posting code.