help with repeating a do while loop

#include<iostream>
#include<string>
using namespace std;

int main()

{
int n1;
int groupsize;
int total = 0;
int total1 = 0;
char userInput;


cout << "Please enter your total number of items \n";
cin >> n1;

cout << "Please enter the group size \n";
cin >> groupsize;

do
{
while(n1>0)
{ int currentGroup = groupsize;
for(int currentGroup = groupsize; n1 > 0 && currentGroup > 0; n1--, currentGroup--)
{
cout << "*";
}
cout << " ";
} cout << endl << "would you like to repeat? " << endl;
cin >> userInput;
switch (userInput)
{case 'Y' : case 'y' : return true;
case 'N' : case 'n' : return false;
}
cout << "please answer Y or N " << endl;


}while(true);





system("pause");
}




*/ Im trying to repeat the while and for loop again so the user can put in a fresh set of numbers again. But so far if I press y or n it exits the program.
closed account (zybCM4Gy)
...who taught you to code?

Never mind, that's our job I guess ;D

You're missing... oh...nuts. That's some...


But this is simple. Sure you've got do while working appropriate...but n1 is always going to evaluate to zero. You can thank the for loop for that.

Better code....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool running = true; //It'll always run if set default to run. 
while( running )
{
   while(n1 > 0) //Rename n1 to something like 'number_of_items'
  {
        for( whatever )
       {
       }
    //gotta reset n1 at some point so...
   cout << "Please enter your total number of items \n";
   cin >> n1;
  } 

   cin >> exit? 
  switch on input
     y: running = false;
    n: running = true;
}


And excuse me.
Last edited on
cout >> "please enter your total number of items \n";
cin >> n;

cout << "please enter the groupsize \n";
cin >> groupsize;

while(running)
{
while(n>0)
{
int currentGroup = groupsize;
for (int currentGroup = groupsize; n> 0 && currentGroup > 0; blah blah)
{
cout <<"*";
}
cout << " ";
}

cout << "exit?" << endl;
cin >> exit;
switch(exit)
y: running = false;
n: running = true;
}
=======================================================

When I implement your way, It still only repeats " exit?" even if I press y or n it just repeats "exit". I'm looking to repeat the while loop and the for loop again. what it does is takes the input numbers and turns em into astrisks.
Example: user inputs 13 and groupsize 5.
the output is ***** ***** ***

after it does this I want to be able to input new numbers and a new set of groupsize after it asks the user if they want to continue.

Thank you for your help and yes my coding does suck... I'm learning from a book :c There is soo much I need to improve on.
Thanks for any amount of time you dedicate to helping a newbie!
p.s how do i get that blue screen you are doing to show me the code? it keeps indenting to the far left margin >.> mine looks horrible!
closed account (iAk3T05o)
You have a couple of problems.
After the while (n>0), you do int currentGroup = groupsize and you also do it in your for loop and there is another problem with the for loop.
Try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
while (running)
{
while (n > 0)
{
for (int currentGroup = groupsize; currentGroup > 0; -- currentGroup)
{
cout << "*";
}
cout << " ";
--n;
}
cout << "Do you want to exit?";
cin >> exit;
switch(exit)
{
case 'n': 
running;
case 'y':
running = false;
}
}

Code tags put it in the blue thingy (code) (/code), replace the () with []
Does the code work?
Last edited on
Topic archived. No new replies allowed.