Can any one find the segmentation fault here, much appriciated

+1 Rehman . · August 28, 2015
Problem Explanation:
User should be asked to enter two integers program should check whethere those integers are under 9 and if they are check what that value is and print that alphabetically, e.g if user entered 2 at first so 2 < 9 and we need to print it "two" and print one element next to it in alphabetic as well, coming along if the input is greater than 9  then program should simply print if its even or odd, 
This thing will be done with the other integer too.
Note: They said For loop is necessary to be used i don't know why but i just used it, so ignore that if you think code can be efficient without for.


#include <iostream>

using namespace std;

int main()
{
    int input[2];
    for(int i = 0; i < 2; i++)
    cin>> input;

    int mod = input[0] % 2;
    int mod2 = input[1] % 2;

    string num[] = {"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

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

        if (input <=9)
        {
            cout<<num[input]<<endl;
            cout<<num[input+1]<<endl;
        }
    }

        if( mod == 0)
            cout<<"even\n";

        if( mod2 == 0)
            cout<<"even\n";

        else
            cout<<"odd\n";

    return 0;
}


Post a Reply

Replies

Oldest  Newest  Rating
0 Montaser Qasem · August 28, 2015
The input is Array so you can input a value like this : cin>>input[0];
you must switch it to cin>>input;
and
if (input<=9)
etc
0 Montaser Qasem · August 28, 2015
here's the code:
http://ideone.com/Dte8aB
0 Rehman . · August 28, 2015
Post meant to find the segmentation fault actually,
nice try but your code is missing the second element's next number as its less than 9. (not concerned)
0 Montaser Qasem · August 28, 2015
Here you go... if that not correct , then i didn't understand your problem please be more specific.

http://ideone.com/Sloxxv
0 c student · August 29, 2015
if (input < = 9)
{
cout<<num[input]<<endl;
   cout<<num[input+1]<<endl;
}


if input is 9, you are accessing input[10] which is out of bounds.
0 Rehman . · August 29, 2015
@C student you are close
0 Rehman . · August 29, 2015
@C student you are close
0 Franz Schmidt · August 29, 2015
Not sure if it's exactly what you want but there we go:
http://textuploader.com/allyd

And a little suggestion by the way: The reason why you should use a for loop is to keep your program dynamic. In your case you wouldn't even need any loop cause there are only two numbers but imagine if you want to input 1000 numbers instead of 2 numbers anytime? You would need to change your whole program to make this work and in my opintion you would only need to change one number :D 

So keep in mind the dynamic of a program is one of the most important things in programming.
  • 1

C++

107,239 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator