Question

Help with C++ reverse program with leading zeros. I need to put the line from the...

Help with C++ reverse program with leading zeros. I need to put the line from the function that display the zeros in main not in the function. So how can I move the display with leading zeros in main. Thanks. Here is my code.

#include <iostream>

#include <cstdlib>

#include <iostream>

using std::cout;

using std::cin;

using std::endl;

//function templates

int reverseNum(int);

int main()

{

//variables

char buf[100];

int num;

while (true)

{

//prompt user for input

cout << "Enter the number to performreverse (q or Q to quit): ";

cin >> buf;

num = atoi(buf);

//quit program if user entered a q

if (buf[0] == 'q' || buf[0] == 'Q') break;

cout << "Entered number: " << num << endl;

cout << "Reversed number: " << reverseNum(num) << endl;

}

}

int reverseNum(int number)

{

int reversedNum = 0;

int reminder;

int zeros = 0;

bool firsttime = true;

while (number > 0)

{

reminder = number % 10;

//If reminder is 0 and the first non zero number is not yet encountered

if (reminder == 0 && firsttime) {

zeros++;

}

//If first non zero digit is encounrered, zeros encountered later should'nt be counted

else {

firsttime = false;

}

reversedNum = reversedNum * 10 + reminder;

number /= 10;

}

cout << "Reversed number With leading zeros : ";

for (int i = 0; i<zeros; i++) {

cout << 0;

}

cout << reversedNum << endl;

return reversedNum;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <cstdlib>

#include <iostream>

using std::cout;

using std::cin;

using std::endl;

//function templates

int reverseNum(int);

int main()

{

//variables

char buf[100];

int num, num1, len = 0, temp, len1 = 0, ret;

while (true)

{

//prompt user for input

cout << "Enter the number to performreverse (q or Q to quit): ";

cin >> buf;

num = num1 = atoi(buf);

while(num1 > 0) //find the length of the number

{

num1 = num1 / 10;

len++;

}

//quit program if user entered a q

if (buf[0] == 'q' || buf[0] == 'Q') break;

cout << "Entered number: " << num << endl;

ret = reverseNum(num); //ret gets the return type from reverseNum function

temp = ret;

while(temp > 0) //find the length of the reversed number

{

temp = temp / 10;

len1++;

}

cout << "Reversed number With leading zeros : ";

for(int i=0; i<(len - len1); i++)

cout << 0;

cout << ret << endl;

cout << "Reversed number: " << ret << endl;

}

}

int reverseNum(int number)

{

int reversedNum = 0;

int reminder;

int zeros = 0;

bool firsttime = true;

while (number > 0)

{

reminder = number % 10;

//If reminder is 0 and the first non zero number is not yet encountered

if (reminder == 0 && firsttime) {

zeros++;

}

//If first non zero digit is encounrered, zeros encountered later should'nt be counted

else {

firsttime = false;

}

reversedNum = reversedNum * 10 + reminder;

number /= 10;

}

/*cout << "Reversed number With leading zeros : ";

for (int i = 0; i<zeros; i++) {

cout << 0;

}

cout << reversedNum << endl;*/

return reversedNum;

}

Terminal -) (82%) w) 3:56 AM Save *Untitled Document 1 zeros.cpp while (true) //prompt user for input cout <Enter the number

Add a comment
Know the answer?
Add Answer to:
Help with C++ reverse program with leading zeros. I need to put the line from the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that takes in a line of text as input, and outputs that line of text in reverse.

    Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text.Ex: If the input is:Hello there Hey quitthen the output is:ereht olleH yeHThere is a mistake in my code I can not find. my output is : ereht olleH ereht olleHyeHHow can I fix this?I saw some people use void or the function reverse but we didnt...

  • CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream>...

    CAN YU HELP ME CONSTRUCT A FLOW CHART FOR THE FOLLOW PROGRAM ? C++ CODE: #include<iostream> using namespace std; int main() { //declaring variable num int num; //prompting user to enter a number cout<<"Input a number to check prime or not:"; //reading input from user cin>>num; //loop to check user input for positive number while(num<0) { cout<<"Error! Positive Integers Only.\n"; cout<<"Input a number to check prime or not:"; cin>>num; } //initializing isPrime variable with false bool isPrime = true; //loop...

  • Bly language program that c l orresponds to the following Cr+ program.Include the memory addr Wri...

    bly language program that c l orresponds to the following Cr+ program.Include the memory addr Write a Pep/9 a include <iostream> using namespace std; int num; int main ( cout << "Enter a number:" cin >> num ; num = num * 4; cout << "num 4-<< num << endl; return 0 bly language program that c l orresponds to the following Cr+ program.Include the memory addr Write a Pep/9 a include using namespace std; int num; int main (...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • Hello everyone! I am working on my assignment for C++ and I'm having a bit of...

    Hello everyone! I am working on my assignment for C++ and I'm having a bit of trouble on the value producing functions, I am creating a program wherein I have to input a value and I need to create a function called square value where in it will square the value I have input. I cannot seem to figure out this part. Here is the code that I have so far, and thank you for any help. I greatly appreciate...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • 15.6: Fix the code to print the count from 1 to x (to loop x times)...

    15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){    for(int i=1; i<=x; i++){ cout<<x; } } int main(){    int x,i;    cin >> x; count(x);    return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...

  • How could I separate the following code to where I have a gradebook.cpp and gradebook.h file?...

    How could I separate the following code to where I have a gradebook.cpp and gradebook.h file? #include <iostream> #include <stdio.h> #include <string> using namespace std; class Gradebook { public : int homework[5] = {-1, -1, -1, -1, -1}; int quiz[5] = {-1, -1, -1, -1, -1}; int exam[3] = {-1, -1, -1}; string name; int printMenu() { int selection; cout << "-=| MAIN MENU |=-" << endl; cout << "1. Add a student" << endl; cout << "2. Remove a...

  • I am having trouble figuring out why my program will not make any columns, just rows....

    I am having trouble figuring out why my program will not make any columns, just rows. I need to display a square. The instructions are provided below. (This for my C++ class) Write a program that displays a square. Ask the user for the square’s size. Only accept positive integer numbers (meaning choose a data type that holds positive integer values). The size the user gives you will determine the length and width of the square. The program should then...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT