Hi,
I am a first semester Computer Engineering student student who is having problems with a for loop.
My assignment is to write in C++ a program that takes an unspecified amount of character input from user and then outputs it 60 characters at a time. So 240 characters input becomes 4 lines of 60 character output. It does this 4 times, Original input, all lowercase, all uppercase, and ASCII encryption (where ' '=~ and vice versa). 60 '-' marks also precede and follow each display. I also must do this without using advanced functions i.e only very simple arrays if any(mostly loops are what we know and very little string, like getline and such). I know I need a for loop but I can't figure out how to make it return after 60 characters. My professor canceled office hours this week so I could not ask him this question. Any help would be greatly appreciated.
Tomdon
rollins720@gmail.com
Without doing your homework for you, it would seem like you need a for loop for each of these functions
1 2 3 4 5 6 7 8
Main loop()
{
for displaying 60 chars(){}
for displaying 60 chars lowercase(){}
for displaying 60 chars uppercase(){}
for displaying 60 chars "ASCII encrypted"(){}
for displaying 60 instances of '-'(){}
}
Since you said an unspecified amount of input, I assume it will be greater than 60 chars, and something you'd want to take before you started the main loop.
Thank you for taking the time to look at my problem. My biggest question is how do I get the program to count the input and return to a new line to continue the output, and not add up the input. I have basically psuedo coded all other for loops except the first one you listed. We did the ASCII encryption as our first program and I nailed that one right on the head.
So, if I understand this correctly, you want essentially want to take input for 60 chars, stop, create a newline, then take more input?
Is that a required function of the program by the class?
My though would be to simply take all of the input into a single string, and do something like this:
1 2 3 4 5 6 7 8 9 10 11
string input;
cin >> input
int chars = 0;
Main loop()
{
for(int i = 0; i < 60 && chars != input.size(); i++)
{
cout << input[chars];
chars++;
}
So chars will essentially be incremented 60 every time the main loop runs, outputing 60 characters at a time. The for loop conditions make sure you don't output something not part of your input string.
Mind you, this is something I have not tested, but it should get you on the right track.
edit: I re-read your post a few times, I'm still a little confused as to what you mean, I hope the post helps. To create a new line after every for loop creates 60 characters of text simply add "cout << endl;" inbetween all of the loops and after the final one. So:
1 2 3 4 5 6 7 8 9 10 11 12 13
Main loop()
{
for displaying 60 chars(){}
cout << endl;
for displaying 60 chars lowercase(){}
cout << endl;
for displaying 60 chars uppercase(){}
cout << endl;
for displaying 60 chars "ASCII encrypted"(){}
cout << endl;
for displaying 60 instances of '-'(){}
cout << endl;
}
Yeah I need it to create a new line but continue with the string of text that the user had to input. So for 61+strings the next line would contain 61-120 characters of input text; next line contain 121 to 180 characters of text and so on. Input just gets truncated into 60 character lines; if 600 characters then 10 lines to redisplay the input. We were shown to use the if (i%60==0) as a measure for going to next line. I am using .length for .size. This is what I have and it returns the first character then the next 59 of first 60 on line below it and no more characters after 60. This program is just going to redisplay the input 4 different ways. I just can't get the thing to return and continue with input after 60 characters.
#include <iostream>
#include <string>
using namespace std;
int chars = 0;
int main()
{string input;
string line="-------------------------------------------------------------";
cout<<"Programmed by "<<endl<<endl;
cout<<"Enter some text and I will encrypt it. Do not press the enter "<<endl;
cout<<"until you are done."<<endl<<endl;
getline(cin, input);
cout<<endl<<line<<endl<<endl;
for(int i = 0; i<=60 && chars != input.length(); i++)
{cout<<input[chars];
chars++;
if(i%60==0)
cout<<endl;}
cout<<endl;
}
So now I got this. This just terminates after the 60 but does not continue the rest. In theory if I input 300 characters it should create 5 full lines of text 60 characters long to display my input in a variety of ways I do understand how to program. I created line b/c after every instance of input I need to have lines b/f and after the repeated output. So will just insert a cout<<line to accomplish that.
#include <iostream>
#include <string>
using namespace std;
int chars=0;
int main()
{
string input;
string line="------------------------------------------------------------";
cout<<"Programmed by "<<endl<<endl;
cout<<"Enter some text and I will encrypt it. Do not press the enter "<<endl;
cout<<"until you are done."<<endl<<endl;
getline(cin, input);
for(chars; chars<=60 && chars != input.length(); chars++)
{
cout<<input[chars];
if(input[chars]%60==0)
cout<<endl;}
cout<<endl;