Question #1: Integer Variables (8 pts)
Write a program that prompts the user for a 5 digit integer, reads the user input into a single integer variable and displays the individual digits of the input in reverse order, one digit per line. For example, the input 13562 is displayed as
2
6
5
3
1
Hint: Use integer divisions.
Restrictions: You are to read the input into one integer variable. You may not convert the number to a string. You may not use selection statements (if).
Assumption: You may assume that the input has no more than five digits and is not negative.
this is what i wrote, but it doesnt work, im new to c++ and i have no clue what to write for this program....
int countDigitsInInteger(int n)
{
int count =0;
while(n>0)
{
count++;
n=n/10;
}
return count;
}
using namespace std;
int main(int argc, char *argv[])
{
int intLength =0;
int number;
int digit;
int sum = 0;
string s;
cout << "Please enter an integer ";
cin >>number;
cout << "Orginal Number = "<<number <<endl;
//make the number positive
if (number<0)
number = -number;
intLength = countDigitsInInteger(number);
//break apart the integer into digits
stack<int> digitstack;
while(number>0)
{
digit = number % 10;
number = number / 10;
digitstack.push(digit);
sum = sum+digit;
}
All looks good except for the stack. "In computer science, a stack is a last in, first out (LIFO)" http://en.wikipedia.org/wiki/Stack_(abstract_data_type)
You don't want LIFO. In the example 12345 the last in is 1 and you don't want 1 to come out first, you want 5 to come out first. You want FIFO, first in first out.
Think about it.
So how would I write that?
stdafx.h gives me an error too. Sorry it's an intro class I'm taking and I'm really lost
I need LIFO and I can't use strings. I'm not sure if what I wrote is write... I doubt it
Look into using vector and the functions push_back. http://www.cplusplus.com/reference/stl/vector
Instead of push onto a stack, push_back on a vector.
Example:
Vec = {}
12345
push_back
Vec = {5}
1234
push_back
Vec = {4, 5}
etc...
Then just loop thru the vector to get them out in the opposite order you pushed them in.
I only modified 5 lines of code (ones that dealt with stack) and added one to get it to work.
I'm on a Mac, so I don't include stdafx.h file. That's a windows file.
i figured it out a different way, now i have another problem.....
Write a program that prompts a user for a text made up of 5 words each ending with a /, and extracts and prints each word without the /. At each step you are required to print the extracted word, the remaining string, and the length of the remaining string. At the end of your program, the variable that contained the string with the 5 words should contain the empty string.
What to do
Here is a general description of what the program is to do.
1. Have a Welcome Banner in which your name(s) appear.
2. Prompt the user for a text
3. Read the string entered by the user into a single variable of type string
4. Extract the first word from the original string, store it into a different string variable, and print out both the extracted word and the remaining string. Include the length of the modified string. When printing out each word, and remaining string print a < at the beginning of the word/string and a > at the end of each. (See sample output in figure 2.)
5. Repeat steps 3 and 4 until you have printed the 5 words and the original string is empty.
6. Send a farewell message, so that the user knows that the program has terminated normally.
7. See sample output screen below. You output does not need to be formatted in exactly the same way. Just make sure the required information appears.
Restrictions: No looping statements allowed.