should return class type data but I get error msg

cfrank2000

Well-known member
Joined
Mar 6, 2021
Messages
71
Programming Experience
Beginner
Hi
I am doing my c# homework, szezonar (sezon price) function should return class type data but I get error
Severity Code Description Project File Line Suppression State
Error CS0161 'Program.szezonar(int, int)': not all code paths return a value szalloda1 C:\Users\Dell\source\repos\szalloda1\szalloda1\Program.cs 32 Active
please help me thank you.

hotelorder1:
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace szalloda1
{
    public class pitypang00
    {
        public int foglaloszam = 0;
        public int szobaszam = 0;
        public int erkezesinapsorszama = 0;
        public int tavozasinapsorszam = 0;
        public int vendegszam = 0;
        public int kernekreggelit = 0;
        public string vendegazonosito = " ";
        public int tartozkodashosz = 0;
        public int teljesar = 0;
    }
    class return1
    {
        public int koz0 = 0;
        public int ar = 0;
        public string st0 = " ";
    }
    class Program
    {
        //public int szezonar(int kezd, int veg, ref int koz0, ref string st0)
        //public int szezonar(int kezd, int veg, out int koz0, out string st0)
        //static return1 szezonar(int kezd0, int veg0, int koz0, int ar, string st0)
        static return1 szezonar(int kezd0, int veg0)
        {
            int tavasz = 9000;
            int nyar = 10000;
            int osz = 8000;
            int koz = 0;

            return1 record0 = new return1();

            if (kezd0 > 1 && kezd0 < 120 && veg0 > 1 && veg0 < 120)
            {
                record0.st0 = " Tavaszi szezon ";
                record0.ar = tavasz;
                record0.koz0 = koz;
                return record0;
            }
            if (kezd0 > 121 && kezd0 < 243 && veg0 > 121 && veg0 < 243)
            {
                record0.st0 = " Nyari szezon ";
                record0.ar =  nyar;
                record0.koz0 = koz;
                return record0;
            }
            if (kezd0 > 244 && kezd0 < 365 && veg0 > 244 && veg0 < 365)
            {
                record0.st0 = " Oszi szezon ";
                record0.ar = osz;
                record0.koz0 = koz;
                return record0;
            }
            if (kezd0 > 1 && kezd0 < 120 && veg0 > 121 && veg0 < 243)
            {
                record0.st0 = " Tavaszi szezon/Nari szezon ";
                record0.ar = veg0-120;
                record0.koz0 = 120 - kezd0;
                return record0;
            }
            if (kezd0 > 121 && kezd0 < 243 && veg0> 244 && veg0 < 365)
            {
                record0.st0 = " Nari szezon/Oszi szezon ";
                record0.ar = veg0-243;
                record0.koz0 = 243 - kezd0;
                return record0;
            }
            if (kezd0 > 244 && kezd0 < 365 && veg0 > 1 && veg0 < 120)
            {
                record0.st0 = " Tavaszi szezon/Nari szezon ";
                record0.ar = 120-veg0;
                record0.koz0 = 365 - kezd0;
                return record0;
            }

        }

        static void Main(string[] args)
        {
            int i = 0;
            int i2 = 0;
            int kod0 = 0;
            int utolso = 0;
            int csere = 0;
            int max = 0;
            int indexmax = 0;

            int erkez = 0;
            int tavoz = 0;
            //int tartozkodas = 0;
            //int koz = 0;
            // string msg = " ";


            string olvas = @"c:\Users\Public\textfiles\pitypang.txt";
            List<pitypang00> adatok = new List<pitypang00>();

            try
            {
                
                // Open the text file using a stream reader.
                using (var sr = new StreamReader(olvas, Encoding.Default))
                {
                    // Read the stream as a string, and write the string to the console.
                    //Console.WriteLine(sr.ReadToEnd());
                    while (!sr.EndOfStream)
                    {

                        string sor = sr.ReadLine();
                        string[] record = sor.Split(' ');

                        adatok.Add(new pitypang00());
                        if (i == 0) { kod0 = Convert.ToInt32(record[0]); }
                        else
                        {   
                            erkez = Convert.ToInt32(record[2]);
                            tavoz = Convert.ToInt32(record[3]);
                            return1 szezon = szezonar(erkez,tavoz);
                            adatok[i].foglaloszam = Convert.ToInt32(record[0]);
                            adatok[i].szobaszam = Convert.ToInt32(record[1]);
                            adatok[i].erkezesinapsorszama = erkez;
                            adatok[i].tavozasinapsorszam = tavoz;
                            adatok[i].vendegszam = Convert.ToInt32(record[4]);
                            adatok[i].kernekreggelit = Convert.ToInt32(record[5]);
                            adatok[i].vendegazonosito = record[6];
                            adatok[i].tartozkodashosz = Convert.ToInt32(record[3])- Convert.ToInt32(record[2]);
                            adatok[i].teljesar = szezon.ar;

                            Console.WriteLine(" i " + i +" " + adatok[i].foglaloszam + " " + record[6] + " " + adatok[i].szobaszam + " " + adatok[i].erkezesinapsorszama);

                        }
                        i++;
                        if (i == 1) { Console.WriteLine(" i " + i + " " + kod0); }
                        
                    }
                    utolso=i;
                    int[] novekvo = new int[utolso];
                    Console.WriteLine(" utolso " + utolso);
                    sr.Close();

                    for (i2 = 1; i2 < utolso; i2++)
                    {
                        Console.WriteLine("  Vendeg ID  " + adatok[i2].vendegazonosito);
                        Console.WriteLine(
                                "  Foglaloszam  " + adatok[i2].foglaloszam +
                                "  Szobaszam   " + adatok[i2].szobaszam +
                                "  Erkezesinap szama  " + adatok[i2].erkezesinapsorszama +
                                "  Tavozasinap szama  " + adatok[i2].tavozasinapsorszam +
                                "  Vendegszam   " + adatok[i2].vendegszam +
                                "  Kernek reggelit   " + adatok[i2].kernekreggelit +
                                "  Tartozkodas hosz " + adatok[i2].tartozkodashosz);

                        novekvo[i2] = i2;
                        
                    }
                    for (i = 1; i < utolso; i++)
                        for (i2 = 2; i2 < utolso; i2++)
                        {
                        
                        if (adatok[novekvo[i2]].tartozkodashosz < adatok[novekvo[i2-1]].tartozkodashosz)
                        {
                            csere = novekvo[i2];
                            //csere2 = novekvo[i2-1];
                            novekvo[i2] = novekvo[i2 - 1];
                            novekvo[i2 - 1] = csere;
                                
                        }
                        
                    }
                    max = adatok[novekvo[utolso - 1]].tartozkodashosz;
                    indexmax = novekvo[utolso - 1];
                    Console.WriteLine("index max " + indexmax+"max " +max);
                    /*
                    for (i = 1; i < utolso; i++)
                    {
                        Console.WriteLine(
                        i+"  Tartozkodas hosz "+ novekvo[i] + " " + adatok[novekvo[i]].tartozkodashosz);
                    }*/
                    for (i = 1; i < indexmax + 1; i++)
                    {
                        if (i == indexmax)
                            {
                            Console.WriteLine(" "+ i +
                                "  Vendeg azonosito  " + adatok[i].vendegazonosito+
                                "  Erkezesinap szama  " + adatok[i].erkezesinapsorszama +
                                "  Leghosszabb tartozkodas  " + adatok[i].tartozkodashosz);
                            }
                    }
                }
            }
            catch (IOException e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
           // Console.WriteLine(" Test "+utolso+ adatok[i2].vendegazonosito);
        }
    }
}
 

Attachments

  • pitypang.txt
    31.8 KB · Views: 14
The C# compiler thinks that with your current code, it's possible for all the if statements to be false (lines 41-82), and so you'll get to the end of the method without returning a class type. What are you missing on line 83?
 
The C# compiler thinks that with your current code, it's possible for all the if statements to be false (lines 41-82), and so you'll get to the end of the method without returning a class type. What are you missing on line 83?
thank you for reply. to be honest I don't see any any mistake, conditions should measure data given on parameter list, that is why I am asking for help. thank you, frank.
 
It doesn't matter that, in practice, one of your if statements will always match. The compiler doesn't know that. As far as the compiler is concerned, it is possible that none of your if statements will match and execution will get to the end of your method and never encounter a return statement. You need to write your code with that in mind. If you should never get to that point then the most appropriate thing to do is to throw an exception. That will satisfy the compiler and it will also warn you if you during testing if you ever mess up and execution actually does get to a point that you didn't intend. If execution never actually gets there then no exception is ever thrown and all is well. If, for instance, you make a mistake in one of your if conditions and you fail to match at some point, the exception will bring that to your attention.
 
to be honest I don't see any any mistake, conditions should measure data given on parameter list
Really? What happens when you call szezonar(-12, 900) ? Which conditions will those values satisfy?
 
Really? What happens when you call szezonar(-12, 900) ? Which conditions will those values satisfy?
thank you for reply. yes that is right, I am sorry I didn't think about it. as you can se , I have a list of data conform that the ' season price' function should return the season price for farther calculation. I guess try catch can be an option or tryparse. can you help me please to sort out this issue? thank you, frank.
 
Just add a return statement on line 83 to return your empty record, or as suggested in post #4, throw an exception. Both will fix your issue.
 
Just add a return statement on line 83 to return your empty record, or as suggested in post #4, throw an exception. Both will fix your issue.
thank you for help. the return part works fine, lovely. if I would need try catch or tryparse what is the right way to avoid mistake? thank you, frank;
 
TryParse() won't do anything for you in that method since you are not doing any parsing. Using try-catch won't help you within that method either since there shouldn't be anything there which would fire of a catchable exception, and even if you did catch the exception, what would you do with it?

Personally, I would prefer to throw an exception instead to indicate that I got into an unexpected condition -- an exceptional condition, if you'll pardon the pun. But it really depends on what your teams standards and coding practices are. Do you guys use the Null Object design pattern to deal with some conditions? Would this be one of those conditions?
 
TryParse() won't do anything for you in that method since you are not doing any parsing. Using try-catch won't help you within that method either since there shouldn't be anything there which would fire of a catchable exception, and even if you did catch the exception, what would you do with it?

Personally, I would prefer to throw an exception instead to indicate that I got into an unexpected condition -- an exceptional condition, if you'll pardon the pun. But it really depends on what your teams standards and coding practices are. Do you guys use the Null Object design pattern to deal with some conditions? Would this be one of those conditions?
that is true, thank you for help, frank.
 
that is true, thank you for help, frank.
I am sorry for coming again. I've got another problem. running the code some values are not passed remaining 0. can I get help?
 

Attachments

  • szalloda1pic2.jpg
    szalloda1pic2.jpg
    248.4 KB · Views: 9
first one is
689 3 241 243 3 1 Szikszay_Maria
and you're passing 241 and 243 (record [2] and [3]), does these values match any of your if statements?
 
first one is

and you're passing 241 and 243 (record [2] and [3]), does these values match any of your if statements?
thank you for reply. there are season ranges: 1 - 120, 121 - 243, 244 - 365. kezd0 means value from beginning, veg0 means last value or termination of stay in hotel. something is wrong, function gives 0 vales on the mentioned records bu I found not passed values also. as a beginner, I don't know yet how to check it.
 
One problem that is easy to spot is you have "< 243" and "> 243" in conditions. That means =243 will fall through. There are <= and >= operators too.
 
One problem that is easy to spot is you have "< 243" and "> 243" in conditions. That means =243 will fall through. There are <= and >= operators too.
thank you for help. I've done some adjustment on szezonar function but the code is still faulty. I was checking summer / autumn ( Nyar/ Osz) but the arrival/leave values still doesn't fall in the right interval. I placed my code below, can I get help please? thank you.
hotelorder1:
return1 record0 = new return1();

            if (kezd0 > 1 && kezd0 < 120 && veg0 > 1 && veg0 < 120)
            {
                record0.st0 = " Tavaszi szezon ";
                record0.ar = tavasz;
                record0.koz0 = koz;
                return record0;
            }
            if (kezd0 > 121 && kezd0 < 243 && veg0 > 121 && veg0 < 243)
            {
                record0.st0 = " Nyari szezon ";
                record0.ar =  nyar;
                record0.koz0 = koz;
                return record0;
            }
            if (kezd0 > 244 && kezd0 < 365 && veg0 > 244 && veg0 < 365)
            {
                record0.st0 = " Oszi szezon ";
                record0.ar = osz;
                record0.koz0 = koz;
                return record0;
            }
            if (kezd0 >= 1 && kezd0 <= 121 && veg0 >= 121 && veg0 < 244)
            {
                if (kezd0 == 121 && veg0 > 121 && veg0 < 244)
                {
                    record0.st0 = " Nari szezon ";
                    record0.ar = nyar;
                }
                else
                {
                    record0.st0 = " Tavaszi szezon/Nari szezon ";
                    record0.ar = 121 - kezd0;
                }
                              
                //record0.koz0 = veg0 - 121;
                return record0;
            }
            if (kezd0 >= 121 && kezd0 <= 243 && veg0 >= 244 && veg0 <= 365)
            {
                if (kezd0 == 243 && veg0 >= 244 && veg0 <= 365)
                {
                    record0.st0 = " Oszi szezon ";
                    record0.ar = osz;
                } else
                {
                    record0.st0 = " Nyari szezon/Oszi szezon ";
                    record0.ar = 244 - kezd0;
                }
                if (veg0 == 243 && kezd0 >= 121 && kezd0 < 243)
                {
                    record0.st0 = " Nyari szezon ";
                    record0.ar = nyar;
                }
                //record0.koz0 = veg0 - 244;
                return record0;
            }
            if (kezd0 >= 244 && kezd0 <= 365 && veg0 > 0 && veg0 < 121)
            {
                record0.st0 = " Tavaszi szezon/Nari szezon ";
                record0.ar = kezd0 - 365;
                record0.koz0 = veg0 -120;
                return record0;
            }
            return record0;
        }
 

Attachments

  • szalloda1pic3a.jpg
    szalloda1pic3a.jpg
    263.5 KB · Views: 12
Back
Top Bottom