Ask your own question, for FREE!
Computer Science 12 Online
OpenStudy (anonymous):

new to c++. I have to write a program that alternates symbols for the number given. For example, if the user inputs the number "6," then the output would be "* @ & * @ *"

geerky42 (geerky42):

So if user input "6", then it should return "* @ & * @ *" ?

OpenStudy (anonymous):

@geerky42 yes

OpenStudy (anonymous):

@geerky42 I'm having trouble with it cause when I put 5 in my program, my input ends up being: * @ & * @ & * @ & * @ & * @ & . And I need it to just be * @ & * @. I don't know how to format it..

geerky42 (geerky42):

Can you show your code?

OpenStudy (anonymous):

#include<iostream> using namespace std; int main() { int number; cout << "Enter a Number: "; cin >> number; if (number <= 0) { cout << number << " is an invalid number." << endl; } for (int i(0); i < number; i += 1) { cout << " * " << " @ " << " & "; } return 0; }

geerky42 (geerky42):

I'm not familiar with C++, but I guess in this line: `cout << " * " << " @ " << " & ";` You basically assign " * @ & " to cout. So for input 5, it will insert " * @ & " to cout five times via for loop. Hence the result will display five " * @ & "s. Maybe use modulo and if, else if, and else statements? Like this: ``` if (i%3==0) { cout << " * "; } else if (i%2 == 0) { cout << " @ "; } else { cout << " & "; } ```

geerky42 (geerky42):

else if (i%2 == 1) ***

geerky42 (geerky42):

Sorry... else if (i%3 == 1) ***

OpenStudy (anonymous):

THE MODULUS WORKED!!! Thank you so much for helping me out! I was stuck on this for 4 hours, embarrassingly...

geerky42 (geerky42):

Ah, I glad I helped :)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!