MSFN Forum: [C++] Help With Loops - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[C++] Help With Loops Rate Topic: -----

#1 User is offline   gamehead200 

  • SEARCH!!! SEARCH!!!
  • Group: Super Moderator
  • Posts: 7,019
  • Joined: 02-September 02
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 09 September 2004 - 08:05 AM

Hello all!

I need some help with a C++ program I'm working on... Its a console program...

Basically, I need to assign a bunch of commands that repeat themselves when the user inputs a certain number into the console... I need to do this without using functions...

Help? :}

Here's the code that has to be repeated:

Quote

    i=0;  // set i back to 0
    cout << "\n*";  // skip line, print *
   
    // loop spaces
    while( i < spaces )
    { 
        cout << " ";
        i = i+1;
    }   
   
    cout << "*";  // print *



#2 User is offline   dtmunir 

  • Member
  • PipPip
  • Group: Members
  • Posts: 163
  • Joined: 17-July 03

Posted 09 September 2004 - 08:18 AM

hmm...gamehead i can definitely help u with C++, but im not sure i get wt ure requirements are....im guessing ur making a simple loop program that will terminate, when the condition i<spaces has been exceeded.
well for this u need 2 things: first of all u need to define spaces, and give it some value, somewhere in ur program, either via user input, or by assigning it some constant value at the start of the program.
second, i dont see the point of the loop, unless the loop is giving some output relating to the number of times it has been run. maybe u shud try putting a " cout << i; " inside the loop.
however if u post ur requirements for the program clearly, ill write the entire code for u. no fret.
cya
dm

#3 User is offline   gamehead200 

  • SEARCH!!! SEARCH!!!
  • Group: Super Moderator
  • Posts: 7,019
  • Joined: 02-September 02
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 09 September 2004 - 08:32 AM

Basically, the "spaces" part needs to become one big command and be repeated x number of times, whatever number the user inputs (this part is easy, I just need to know how to make it one command). I have to assign the above code to a variable or something... That's what I don't understand, and my teacher doesn't want me to use functions...

Maybe you will understand the code with the rest of it:

Quote

#include <iostream>
#include <string>

using namespace std;

int main()
{

    // asks for your name
    string name;
    cout << "What is your name?\n";
    cin >> name;
    cout << "\n";
   
    // define variables
    int stars, spaces, i;
    stars = name.size()+4;
    spaces = name.size()+2;
    
    i = 0;  // set i to 0
   
    // loop *s
    while( i < stars )
    { 
           cout << "*";
           i = i+1;
    }   
    // end first loop
   
    i=0;  // set i back to 0
    cout << "\n*";  // skip line, print *
   
    // loop spaces
    while( i < spaces )
    { 
           cout << " ";
           i = i+1;
    }   
   
    cout << "*";  // print *    
    cout << "\n* " << name << " *\n";  // skip line, print *, space, print *, skip line
   
    i=0;  // hmmmm... guess, what did i do above?
    cout << "*";  // print *
   
    // loop spaces
    while( i < spaces )
    { 
           cout << " ";
           i = i+1;
    }   
   
    cout << "*";  // print *
i = 0;  // guess again...
cout << "\n";  // skip line

// loop stars
    while( i < stars )
    { 
           cout << "*";
           i = i+1;
    }
    
    cout << "\n\n";
    system("PAUSE");
    return 0;
}


#4 User is offline   dtmunir 

  • Member
  • PipPip
  • Group: Members
  • Posts: 163
  • Joined: 17-July 03

Posted 09 September 2004 - 09:06 AM

hey man. im still not 100% clear on ur problem. but if im gettin it correctly then the following small program that i cooked up for u shud help.

basically all this does is ask the user the number of spaces they want to print, and then prints tht many spaces in between some text. u can tweak it to ur suitability.

Quote

#include "stdafx.h"  // not sure if u really need this, but my compiler sticks it in anyways.
#include<iostream>
#include<string>
using namespace std;
int main(){
string myspaces;
int num,i;
cout << "enter the number of spaces u want to print: ";
cin >> num;
i=0;
myspaces="";
while (i<num){
  myspaces=myspaces.append(" "); //adds a space at the end of the string myspaces
  i++; //increases i by 1
}
cout << "this is " << myspaces << " many spaces";
cin >> i;
return 0;
}


however, if this ISNT wt u were looking for, then pls add me on msn, dtmunir@msn.com and maybe i can understand ur requirements better.
cheers
danish

#5 User is offline   HeartsOfWar 

  • Newbie
  • Group: Members
  • Posts: 49
  • Joined: 25-February 04

Posted 09 September 2004 - 10:15 AM

Maybe, in english, type the problem that was given to you...

This way we can read the description and work it on our own...

#6 User is offline   gamehead200 

  • SEARCH!!! SEARCH!!!
  • Group: Super Moderator
  • Posts: 7,019
  • Joined: 02-September 02
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 09 September 2004 - 10:41 AM

OK, I'll explain it ONE more time! :P

The entire code I posted surrounds stars around a word (compile it to see it in action)... My programming teacher wants me to take the part that prints out the spaces (the first piece of code), and make it so that the program asks the user how many spaces (vertically) the user wants in between the word and the top stars. HOWEVER, he wants the first piece of code in my first post to only appear ONCE in the entire code, and only have it repeated by the number the user inputs where it must go! :wacko:

Edit: Actually, you might have gotten me on the right track... ;)

#7 User is offline   dtmunir 

  • Member
  • PipPip
  • Group: Members
  • Posts: 163
  • Joined: 17-July 03

Posted 09 September 2004 - 12:04 PM

u want vertical spaces...thts very simple....just use the following modification to my code:


myspaces.append("\n");

put this inside the loop and it should work fine.
do lemme know if it worked
and my offer for emailin me, or msn'ing me still stands
cya

#8 User is offline   gamehead200 

  • SEARCH!!! SEARCH!!!
  • Group: Super Moderator
  • Posts: 7,019
  • Joined: 02-September 02
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 09 September 2004 - 04:43 PM

Yes, I want vertical spaces, HOWEVER, there MUST be an asterisk on each end... So lets say I enter GAMEHEAD200 as my word or name, there are 11 characters. Add 1 space on each end, plus an asterisk. Therefore, for the vertical space, there must be a total of 15 characters, 13 being spaces... Get where I'm going? :}

#9 User is offline   dtmunir 

  • Member
  • PipPip
  • Group: Members
  • Posts: 163
  • Joined: 17-July 03

Posted 10 September 2004 - 04:22 AM

yeh. so thats not so bad. lets try this.

int spaces,i;
string name, myspaces;

cout << "Enter name";
cin >> name;

spaces=name.size() + 2;
myspaces="";
myspaces.append("*\n");

i=0;
while (i<spaces){
myspaces.append("\n");
i=i+1; // u can simply use i++ also
}

myspaces.append("*\n");

hows this for u?

#10 User is offline   gamehead200 

  • SEARCH!!! SEARCH!!!
  • Group: Super Moderator
  • Posts: 7,019
  • Joined: 02-September 02
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 13 September 2004 - 09:11 AM

Yay! I got it! I just had to put a loop in a loop... So simple! ;)

Enjoy the code if you want! :D

#include <iostream>
#include <string>

using namespace std;

int main()
{

    // asks for your name
    string name;
    int starspace, space1;  // define spaces
    cout << "Please enter your first or last name.\n";
    cin >> name;
    cout << "\n";
    
    // asks for how many spaces you want
    cout << "How many spaces would you like between your name and the stars?\n";
    cin >> starspace;
    space1 = starspace;  // create a second variable for starspace
    cout << "\n";
    
    // define variables
    int stars, spaces, i, s;
    stars = name.size()+(space1*2)+2;
    spaces = name.size()+(space1*2);
    
    i = 0;  // set i to 0
    // loop stars
    while( i < stars )
    {  
           cout << "*";
           i = i+1;
    }    
    // end loop stars
    
    s = 0;  // set s to 0
    // loop the space lines
    while( s < starspace )
    {
        ////////////////////////
        
        i = 0;;  // set i back to 0
        cout << "\n*";  // skip line, print *
        
        // loop spaces
        while( i < spaces )
        {  
               cout << " ";
               i = i+1;
        }    
        
        cout << "*";  // print *
                
        ////////////////////////
        s = s+1;
    }
    // end loop space lines
    
    cout << "\n*";  // skip line, print *
    
    i = 0;  // set i to 0
    // loop spaces
    while( i < space1 )
    {  
           cout << " ";
           i = i+1;
    }    
    // end loop spaces
    
    cout << name;  // print name
    
    i = 0;  // set i to 0
    // loop spaces
    while( i < space1 )
    {  
           cout << " ";
           i = i+1;
    }    
    // end loop spaces
    
    cout << "*\n";  // print *, skip line
    
    s = 0;  // set s back to 0
    // loop space lines
    while( s < starspace )
    {
        ////////////////////////
        
        i = 0;  // set i back to 0
        cout << "*";  // skip line, print *
        
        // loop spaces
        while( i < spaces )
        {  
               cout << " ";
               i = i+1;
        }    
        
        cout << "*\n";  // print *, skip a line
                
        ////////////////////////
        s = s+1;
    }
    // end loop space lines
    
	i = 0;  // guess again...
	// loop stars
    while( i < stars )
    {  
           cout << "*";
           i = i+1;
    }
    // end loop stars
     
    cout << "\n\n";
    system("PAUSE");
    return 0;
}


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy