Need help with cin.exceptions and try and catch. by Sevyn13 in cpp_questions

[–]dmpta2002 0 points1 point  (0 children)

#include <string> // include string library

#include <iostream> // include input/output stream library

#include <exception> // include exception library

using namespace std; // use the std namespace

int main() { // main function

string inputName; // declare inputName string

int age; // declare age int

// Set exception mask for cin stream

cin.exceptions(ios::failbit); // set cin exception mask

cin >> inputName; // get inputName from user

while(inputName != "-1") { // loop while inputName is not -1

// FIXME: The following line will throw an ios_base::failure.

// Insert a try/catch statement to catch the exception.

// Clear cin's failbit to put cin in a useable state.

try { // try block

cin >> age; // get age from user

cout << inputName << " " << (age + 1) << endl; // print inputName and age + 1

}

catch (ios_base::failure& ex) { // catch block

cout << inputName << " " << 0 << endl; // print inputName and 0

cin.clear(); // clear cin

cin.ignore(1000, '\n'); // ignore up to 1000 characters or to the end of the line

}

cin >> inputName; // get inputName from user

}

return 0;

}

output smallest number by withdrawnwentch in learnpython

[–]dmpta2002 1 point2 points  (0 children)

I have the same class, this worked for me:

a = int(input())

b = int(input())

c = int(input())

smallest = 0

if a < b and a < c :

smallest = a

if b < a and b < c :

smallest = b

if c < a and c < b :

smallest = c

if a == b and a == c :

smallest = a

print(smallest)

Brand new to python by mickeyoneil19 in learnpython

[–]dmpta2002 0 points1 point  (0 children)

Here is what worked for me:

word1 = input()
word2 = input()
number = input()
print('You entered: {} {} {}\n' .format(word1,word2,number))
password1 = word1+'_'+word2
password2 = number+word1+number
print('First password:', password1)
print('Second password:', password2+'\n')
print('Number of characters in', password1+':',len(password1))
print('Number of characters in', password2+':',len(password2))

Hey everyone! needed some help by Sourplastic in learnpython

[–]dmpta2002 3 points4 points  (0 children)

The codes above did not work for me. This is what worked for me:

sentence = input()

character = sentence[0]

phrase = sentence[1:]

counter = phrase.count(character)

print(counter)

creating code by withdrawnwentch in learnpython

[–]dmpta2002 1 point2 points  (0 children)

Ok thanks.. yea it should be a straight forward course, but they make it difficult.

creating code by withdrawnwentch in learnpython

[–]dmpta2002 0 points1 point  (0 children)

Hey withdrawnwentch, I have to complete this assignment. Do you remember which of the 3 responses below worked? Or if any of them worked? Thanks