Question
This is C++ I only can change the lines highlighted in white (i can add lines to it with enter though)

Set hasDigit to true if the 3-character passCode contains a digit. 1 #include <iostream» 2 #include <string> 3 #include <cctype> 4 using namespace std; 6 int main 7 bool hasDigit; 8 string passCode; 10 hasDigit false; 11 cin passCode; 12 13 Your solution goes here 14 15 if ChasDigit) 16 cout << Has a digit. < endl; 17 18 else cout s Has no diait endl:
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

As you say you can only change the highlighted line. I will take a precaution that length of paaCode must be greater than 3.

I am trying to explain in comments but stil if you not understand feel free to ask in comments.

So here is your modified code:-

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
   bool hasDigit;
   string passCode;
  
   hasDigit = false;
   cin>>passCode;
  
   // My solution Start
   int length = passCode.length();   //get length of string
   for(int i=0;i<length;i++)
   {
       int digit = passCode[i]-'0';   //its a trick to convert a character to number
       if(digit>=0 && digit <10)   //since digit is between 0 to 9
       {
           hasDigit = true;
       }
   }
  
  
   //Mysolution Ends
   if(hasDigit)
   {
       cout<<"Has a digit."<<endl;
   }
   else
   {
       cout<< "Has no digit."<<endl;
   }
   return 0;
}

Here is how output appears:

Best of luck for your future...

Add a comment
Know the answer?
Add Answer to:
This is C++ I only can change the lines highlighted in white (i can add lines...
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
  • 3.14.1: String with digit. Set hasDigit to true if the 3-character passCode contains a digit. HELP...

    3.14.1: String with digit. Set hasDigit to true if the 3-character passCode contains a digit. HELP ME FIX THIS #include <iostream> #include <string> #include <cctype> using namespace std; int main() { bool hasDigit; string passCode; hasDigit = false; cin >> passCode; for(int i=0;i<passCode.length();i++) { if(isdigit(passCode[i])) { hasDigit=true; break; } } if (hasDigit) { cout << "Has a digit." << endl; } else { cout << "Has no digit." << endl; } return 0; }

  • CHALLENGE ACTIVITY 8.4.2: Find char in C string Assign a pointer to any instance of searchChar...

    CHALLENGE ACTIVITY 8.4.2: Find char in C string Assign a pointer to any instance of searchChar in personName to searchResult. #include <iostream> #include <cstring> using namespace std; int main() { char personName[100]; char searchChar; char* searchResult = nullptr; cin.getline(personName, 100); cin >> searchChar; /* Your solution goes here */ if (searchResult != nullptr) { cout << "Character found." << endl; } else { cout << "Character not found." << endl; } return 0; }

  • In C Set hasDigit to true if the 3-character passCode contains a digit. 1 #include «stdio.h>...

    In C Set hasDigit to true if the 3-character passCode contains a digit. 1 #include «stdio.h> 2 #include«string.h> 3 #include «stdbool.h 4 #include<ctype.h> 6 int main(void) 7 bool hasDigit; 8 char passCode501; 10 11 12 hasDigit false; scanf("%s", &passcode); 13 Your solution goes here * 14 15 if (hasDigit) 16 printf("Has a digit.n 4 #include <ctype.h> 6 int main (void) 1 8 char passCode [50]; bool hasDigit; hasDigit- false; 10 11 scanf("%s",&passcode); 12 13 14 15 if (hasDigit) 16 17...

  • C++ Write a function so that the main() code below can be replaced by the simpler...

    C++ Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). Original main(): int main() { double milesPerHour; double minutesTraveled; double hoursTraveled; double milesTraveled; cin >> milesPerHour; cin >> minutesTraveled; hoursTraveled = minutesTraveled / 60.0; milesTraveled = hoursTraveled * milesPerHour; cout << "Miles: " << milesTraveled << endl; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #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> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • *HOW DO I CHANGE THIS FROM A VOID FUNCTION TO A NON-VOID WITH PARAMETERS?* #include<iostream> #include<fstream>...

    *HOW DO I CHANGE THIS FROM A VOID FUNCTION TO A NON-VOID WITH PARAMETERS?* #include<iostream> #include<fstream> #include<string> using namespace std; void studentStats() { ifstream inputFile; inputFile.open("outFile.txt"); string studentData; string studentID; string ID, exam1, exam2, exam3; string header; cout << "Enter a Student ID: "; cin >> studentID; bool found =false; while (inputFile) { inputFile >> ID; inputFile >> exam1; inputFile >> exam2; inputFile >> exam3; if (ID.compare(studentID)==0) { cout << ID << " " << exam1 << " " <<...

  • 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...

  • When I try to run the program it gives me an erron saying Expected expresion on...

    When I try to run the program it gives me an erron saying Expected expresion on this line, else if(phrase.find(sub_String)!=std::string::npos). #include #include #include using namespace std;    int main( ) {          // Defining variables. //Initilizing variables. int magicNum; int magicNum1; int position; int phraseLenght; string vowels = "aeiou"; string phrase; string sub;       // The phrase and the sub phrase cout << "Enter a phrase:" << endl; std::getline (std::cin,phrase);    cout << "Enter a 3-character sub:"...

  • Coding in c++

    I keep getting errors and i am so confused can someone please help and show the input and output ive tried so many times cant seem to get it. main.cpp #include <iostream> #include <vector> #include <string> #include "functions.h" int main() {    char option;    vector<movie> movies;     while (true)     {         printMenu();         cin >> option;         cin.ignore();         switch (option)         {            case 'A':            {                string nm;                int year;                string genre;                cout << "Movie Name: ";                getline(cin, nm);                cout << "Year: ";                cin >> year;                cout << "Genre: ";                cin >> genre;                               //call you addMovie() here                addMovie(nm, year, genre, &movies);                cout << "Added " << nm << " to the catalog" << endl;                break;            }            case 'R':            {                   string mn;                cout << "Movie Name:";                getline(cin, mn);                bool found;                //call you removeMovie() here                found = removeMovie(mn, &movies);                if (found == false)                    cout << "Cannot find " << mn << endl;                else                    cout << "Removed " << mn << " from catalog" << endl;                break;            }            case 'O':            {                string mn;                cout << "Movie Name: ";                getline(cin, mn);                cout << endl;                //call you movieInfo function here                movieInfo(mn, movies);                break;            }            case 'C':            {                cout << "There are " << movies.size() << " movies in the catalog" << endl;                 // Call the printCatalog function here                 printCatalog(movies);                break;            }            case 'F':            {                string inputFile;                bool isOpen;                cin >> inputFile;                cout << "Reading catalog info from " << inputFile << endl;                //call you readFromFile() in here                isOpen = readFile(inputFile, &movies);                if (isOpen == false)                    cout << "File not found" << endl;...

  • In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double...

    In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double length; double height; double tailLength; string eyeColour; string furClassification; //long, medium, short, none string furColours[5]; }; void initCat (Cat&, double, double, double, string, string, const string[]); void readCat (Cat&); void printCat (const Cat&); bool isCalico (const Cat&); bool isTaller (const Cat&, const Cat&); #endif ***//Cat.cpp//*** #include "Cat.h" #include <iostream> using namespace std; void initCat (Cat& cat, double l, double h, double tL, string eC,...

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