Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:10:06+00:00 2026-06-12T13:10:06+00:00

The exercise asks for a code which can convert the user input of the

  • 0

The exercise asks for a code which can convert the user input of the numbers 0-9 either as an integer or string to a string or integer respectively i.e. if0 is entered "zero" will be outputted and vice versa.

string number;

cout << "Let's convert strings to numbers."
     << "Enter value/string of 0-9";

while (number!= "exit")
{
    cin >> number;

    for (int i=0; i < digits.size(); i++) 
    {
        if (number == digits[i]) cout << i << endl;
    }   


    if (number == "0")      cout << digits[0] << endl;
    else if (number == "1") cout << digits[1] << endl;
    else if (number == "2") cout << digits[2] << endl;
    else if (number == "3") cout << digits[3] << endl;
    else if (number == "4") cout << digits[4] << endl;  
    else if (number == "5") cout << digits[5] << endl;  
    else if (number == "6") cout << digits[6] << endl;  
    else if (number == "7") cout << digits[7] << endl;
    else if (number == "8") cout << digits[8] << endl;  
    else if (number == "9") cout << digits[9] << endl;  
}

digits is a vector class which stores the strings "zero", "one" etc.

This code works fine but I don’t like the long chain of if/else if statements but I can’t figure out a way to convert the integers to strings. Can someone help me make this more efficient? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:10:08+00:00Added an answer on June 12, 2026 at 1:10 pm

    you can use that if number == "0" then number[0] == '0' which is char.

    e.i instead if/else statements:

    if (number[0] >= '0' && number[0] <= '9' )
      std::cout << digits[number[0] - '0'] << std::endl;
    else
      std::cout << "wrong input - needs to be digit" << std::endl;
    

    string is basically an array of characters, std::string is an array of characters of type char.
    For instance these are two legimate ways to declare and initialize strings in c or c++

    char s[3] = { '0', '1', '\0' }; 
    char s[3] = "01";
    

    Char value is technically integer (or rather byte) that stores the character code in some encoding (usually ASCII).
    For instance the character code of ‘0’ is 48, that of ‘1’ is 49, ‘2’ is 50. And we use this, because we know that

    '3' - '0' = 51 - 48 = 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing code for the exercise 1-24, K&R2, which asks to write a basic
Can anyone help with this exersise? Write a program which asks the user for
I am doing an exercise in which the user has to input a signed
Exercise 14.16-17 in Thompson asks me to add the operations of multiplication and (integer)
In the code exercise at Codecademy[1], it asks you to cube a variable and
I am working on an exercise which asks me to take a base class
One of the exercises in TC++PL asks: Write a function that either returns a
The exercise says: Create a Text class that contains a string object to hold
I have the exercise with following code int FindFirstSet(unsigned BitMap, unsigned start) { unsigned
As part of an exercise, am implementing an ArrayList which will support Enumerations. Following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.