String help

For my class I was asked to create some code to be able to accept input for 3 types of numbers: one that is divisible by 3, by 5, and by both 3 and 5. That all went fine using if else statements, but now I am having trouble expanding the program to accept multiple numbers separated by commas. I am thinking that this can be solved with strings but I am not sure how to set it up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 #include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main()
{
	int number;
	cout << "Please input a number and press the enter key" << endl;
	cin >> number;
	
	if (number % 15 == 0){
		cout << "FizzBuzz" << endl;/*FizzBuzz - do this one first as my `if` block is not mutually exclusive*/
	}
	else if (number % 3 == 0){
		cout << "Fizz" << endl;/*Fizz*/
	}
	else if (number % 5 == 0){
		cout << "Buzz" << endl;/*Buzz*/
	}
	else
		cout << "Please Select a different number" << endl; /*The number entered is neither divisible by 3 or 5*/

	return 0;
}
I think you are close.

you just need to test if 3 % == 0 and 5 % == 0. eg the && operator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <iostream>

 using namespace std;

 int main() {
	 int number;
	 cout << "Please input a number and press the enter key" << endl;
	 cin >> number;
	 if (number % 5 == 0 && number % 3 == 0) {
		 cout << "FizzBuzz" << endl;/*FizzBuzz - do this one first as my `if` block is not mutually exclusive*/
	 }
	 else if (number % 3 == 0) {
		 cout << "Fizz" << endl;/*Fizz*/
	 }
	 else if (number % 5 == 0) {
		 cout << "Buzz" << endl;/*Buzz*/
	 }
	 else
		 cout << "Please Select a different number" << endl; /*The number entered is neither divisible by 3 or 5*/

	 return 0;
 }
I've written the parsing code for you. Simply integrate your code into it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 #include <iostream>
#include <cmath>
#include <string>
#include <sstream>
using namespace std;

int StringToInt(string str)
{
    stringstream convert(str);
    int num;
    convert >> num;
    return num;
}

int main()
{
	string strNum = "";
	string str = "";
	int number = 0;

	cout << "Please input a series of numbers separated by commas: ";
	getline(cin, str);
	for(int i = 0; i<str.length(); ++i)
	{
	   while (str[i] != ' ' &&  str[i] != ',')
	    {
	        strNum += str[i];
	        i++;
	    }

	   if(strNum != "" || i == str.length()-1)
             {
	        number  = StringToInt(strNum);
	        cout << number << " "; // replace this cout statement with lines 12-22 of your code
	        strNum = "";
             }

	}
}
Last edited on
I am able to get the program to react accordingly for the first number entered, but lets say the user inputs 8,10,25. I would like the program to then output "8 - Please Select another number, 10 - Buzz, 25 - FizzBuzz."
I am able to get the program to react accordingly for the first number entered

What do you mean here?

but lets say the user inputs 8,10,25. I would like the program to then output "8 - Please Select another number, 10 - Buzz, 25 - FizzBuzz."


So you just want to print the number along with the message?
Last edited on
As it stands if the user inputs just a 10 it will output "10 - Buzz". However, if you input multiple numbers it will still just output "10 - Buzz." I am looking for the proper output for each input. Does that make better sense?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<cstring>
#include <sstream>

int main()
{
    const char comma = ',' ;

    std::cout << "enter a sequence of numbers separated by commas on a single line\n" ;

    std::string line ;
    std::getline( std::cin, line ) ; // read the complete line as a string
    std::cout << "you entered: " << line << '\n' ;
    
    line += comma ; // append a comma at the end; this will make our processing simpler

    std::istringstream strstm(line) ; // construct a string stream which can read integers from the line

    int number ;
    char separator ;
    while( strstm >> number >> separator && separator == comma ) // for each number read from the line
    {
        const bool divisible_by_3 = number%3 == 0 ;
        const bool divisible_by_5 = number%5 == 0 ;

        std::cout << number << " - " ;
        
        if( divisible_by_3 && divisible_by_5 ) std::cout << "both a fizz and a buzz\n" ;
        else if( divisible_by_3 ) std::cout << "only a fizz\n" ;
        else if( divisible_by_5 ) std::cout << "only a buzz\n" ;
        else std::cout << "no fizz and no buzz\n" ;
    }
}

http://coliru.stacked-crooked.com/a/5c5c27b4686db6e1
JLBorges,

Your solution worked perfectly! Thank you so much! :)
Strange, ran my code and it prints out the number and message for each number, not just the first.
Topic archived. No new replies allowed.