Switch Code

Hey guys,

i am new to C++ programming and I have a task. I need to create a code so that people can input what year they started school and then it will calculate what year they are in.

I am confident with everything up to scanf (where they input what year they started school)

I do not know how to write the code so it calculates what school year they are in. Any help?

cheers
scanf is a C function (though it is available in C++), whereas in C++ we use std::cin -why are you trying to use C functions if you have access to C++?

You will want to look into the date and time libraries in C++. Some Googling will work.
In our school, they are starting from the basics, that is why we have scanf.

Instead of cout, we use printf as well. I just need to know how to create something that calculates what year students are in from them inputting what year they started
http://www.cplusplus.com/reference/ctime/

I would argue that printf and scanf are not basic when you are using C++, but I can do nothing to control your poorly structured class. You'll just have to suffer through it, unfortunately.
Last edited on
hey,

i saw your link, what library would you need to insert? I have my code below. To make it more accurate, I want something that gives you the current date.

I want something that says if the current date is September of earlier, you will be in Year x, but if it is after September, then you would be in Year x+1.

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

int main(void)
{
int year;

printf("\nWhat year did you start SGS? : ");
scanf("%d", &year);

int current;
current = (2014 - year)+7;
printf("You are in Year: :%d", current);



return 0;
}
Just FYI, your code is pure C. If your class claims to be teaching C++, it's not.

You need to put your code [code]between code tags[/code] so that it gets fancy syntax highlighting:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main(void)
{
    int year;

    printf("\nWhat year did you start SGS? : ");
    scanf("%d", &year);

    int current;
    current = (2014 - year)+7;
    printf("You are in Year: :%d", current);

    return 0;
}
On line 13, you hard-code the current year as 2014, meaning that your program will stop working correctly in just a few months.
KrtinN wrote:
I want something that says if the current date is September of earlier, you will be in Year x, but if it is after September, then you would be in Year x+1.
Again, you're hard-coding a month. Your program will not work next month.

As for which header to include - it's at the top of the page I linked in big bold text and ends in ".h"

There's an example for each link on the page - for example, this one:
http://www.cplusplus.com/reference/ctime/time/
Last edited on
Topic archived. No new replies allowed.