vendredi 2 juin 2017

C++- If statement issue. Code will not run the else or else if

I will enter my code below. As a disclaimer I am a complete and utter novice, i know my code is terribly laid out ect and I should be creating functions... For now I'm keeping it simple.

My problem is that my if statement will run the "if" part, but it will not run the "else if" part. Any help is appreciated.

My code:

// Coding Challenges.cpp : Defines the entry point for the console 
application.
//
#include "cstdlib"
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

char responses(char user_response)
{
return user_response;
}

int main()
{
char responses;
char user_response;
cout << "Welcome to the tempature conversion program." << endl;
cout << "Please type F to convert from farenheit to celcius or C for celcius 
to farenheit." << endl;
//getchar(user_response);
cin >> user_response;
getchar();
cout << "You entered: " <<user_response << endl;


switch (user_response) {
case 'F':
    cout << "You have chosen to convert from farenheit to celcius." << endl;
    break;

case 'C':
    cout << "You have chosen to convert from celcius to farenheit." << endl;
    break;

default:
    cout << "Woops. It Looks like you haven't entered correctly." << endl;
    break;
}
getchar();
cout << "Code should have ran " << endl;
getchar();
cout << "You entered: " << user_response << endl;
getchar();

if (user_response = 'F') {
    double f_val = 0;
    cout << "Please enter the value of farenheit you would like to convert: 
" << endl;
    cin >> f_val;
    cout << f_val;
    getchar();
    getchar();


} else if (user_response = 'C') {
    double c_val = 0;
    cout << "Please enter the value of celcius you would like to convert: " 
<< endl;
    cin >> c_val;
    cout << c_val;
    getchar();
    getchar();


} else {
    cout << "Error" << endl;
    return 0;
    getchar();
    getchar();
}
getchar();
getchar();

return 0;
}

Ignore any code i have obviously used for testing/ debugging.

Thanks,

Ted

Aucun commentaire:

Enregistrer un commentaire