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 "* @ & * @ *"
So if user input "6", then it should return "* @ & * @ *" ?
@geerky42 yes
@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..
Can you show your code?
#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; }
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 << " & "; } ```
else if (i%2 == 1) ***
Sorry... else if (i%3 == 1) ***
THE MODULUS WORKED!!! Thank you so much for helping me out! I was stuck on this for 4 hours, embarrassingly...
Ah, I glad I helped :)
Join our real-time social learning platform and learn together with your friends!