r/programminghelp • u/Admiralbenbow123 • May 16 '21
C++ C++ ( MS Visual Studio) University assignment: I have to create a function named "sogl" that outputs 1 when it receives a consonant letter and 0 when it receives a vowel letter. For some reason it always outputs 1 and I can`t tell why.
Code:
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
using namespace std;
const char e = 'e';
const char y = 'y';
const char u = 'u';
const char il = 'i';
const char o = 'o';
const char a = 'a';
int i, z, n;
int sogl(char str)
{
if (str == e || str == y || str == u || str == il || str == o || str == a) {
i = 0;
}
else {
i = 1;
}
return i;
}
int main() {
char str[1];
scanf_s("%s", str, 1);
printf_s("\n");
z = sogl(str[0]);
printf_s("%d", z);
}2
u/colonelflounders May 17 '21
So the answer you got so far should get you on the right track. I want to add to it though.
If you aren't comfortable using a debugger, I'm in that category myself as I have gotten by with other methods, you can verify the input that is going into the function first to see if you are getting the expected behavior. One way of doing this is printing the function argument first separately before the call. For functions that simply map an input to an output this approach is just fine. This approach breaks down when you have a lot of shared state, but it can still be made to work. Do learn to use the debugger, you really need it for embedded work, but print debugging is another tool at your disposal that is simple.
You don't have to test your function with input from stdin. You can just pass in some characters as test cases to see if the function is fine. An example of doing this is as simple as the following:
void test() {
printf("%c %d\n", 'a', sogl('a'));
}You can even do that from main, it doesn't have to be in a function called test. If you don't want a bunch of printing happening to test a function, you can also use an assert that will effectively crash the program; I haven't used them in C++, and for C I don't remember currently how to, but that's another tool that you have for tackling these things without flooding your program output.
Hopefully this was helpful. I'd be happy to explain more if I wasn't clear.
1
u/Admiralbenbow123 May 17 '21
Thanks for the advice! I'm somewhat fine with using a debugger but I'll keep this in mind
3
u/[deleted] May 16 '21 edited May 16 '21
You're using Visual Studio and native windows compiling, so you need to take advantage of the built-in tools available to you.
Putting your cursor on scanf_s and pressing F1 would show you the official documentation of the function.
If you put a breakpoint on the quoted line above, and step into the sogl function, you would see that the char you're passing in, every time, is a null terminator.
As it stands you're taking in a string, which will include a null terminator. This makes the incoming input two characters big (if you press one letter and hit enter). But you're asking for one character back, so you're getting back the null terminator every time.
Your options are:
Change the 1 to a 2, but leave the str array at a size of 1. This will take the input in as a null terminated string but insert the character you pressed into the array and ignore the null terminator.
Change %s to %c, since you're trying to capture a single character anyway. This will take the character you press (instead of the null terminated string) effectively ignoring the null terminator from before and everything you have should 'just work'.