gamehead200 Posted September 9, 2004 Share Posted September 9, 2004 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: i=0; // set i back to 0 cout << "\n*"; // skip line, print * // loop spaces while( i < spaces ) { cout << " "; i = i+1; } cout << "*"; // print * Link to comment Share on other sites More sharing options...
dtmunir Posted September 9, 2004 Share Posted September 9, 2004 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.cyadm Link to comment Share on other sites More sharing options...
gamehead200 Posted September 9, 2004 Author Share Posted September 9, 2004 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:#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;} Link to comment Share on other sites More sharing options...
dtmunir Posted September 9, 2004 Share Posted September 9, 2004 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.#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.cheersdanish Link to comment Share on other sites More sharing options...
HeartsOfWar Posted September 9, 2004 Share Posted September 9, 2004 Maybe, in english, type the problem that was given to you...This way we can read the description and work it on our own... Link to comment Share on other sites More sharing options...
gamehead200 Posted September 9, 2004 Author Share Posted September 9, 2004 OK, I'll explain it ONE more time! 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! Edit: Actually, you might have gotten me on the right track... Link to comment Share on other sites More sharing options...
dtmunir Posted September 9, 2004 Share Posted September 9, 2004 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 workedand my offer for emailin me, or msn'ing me still standscya Link to comment Share on other sites More sharing options...
gamehead200 Posted September 9, 2004 Author Share Posted September 9, 2004 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? Link to comment Share on other sites More sharing options...
dtmunir Posted September 10, 2004 Share Posted September 10, 2004 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? Link to comment Share on other sites More sharing options...
gamehead200 Posted September 13, 2004 Author Share Posted September 13, 2004 Yay! I got it! I just had to put a loop in a loop... So simple! Enjoy the code if you want! #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;} Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now