Like Tree3Likes

I want to convert this C program to C#

This is a discussion on I want to convert this C program to C# within the C# Programming forums, part of the General Programming Boards category; Hello gurus on forum I was wondering if ya could help me I'm wanting to convert this c program to ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    150

    I want to convert this C program to C#

    Hello gurus on forum I was wondering if ya could help me I'm wanting to convert this c program to c#

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      int ch;
      int num_chars = 0;
      int num_lines = 0;
      
      printf("\nThis program counts the number of characters");
      printf("\nyou enter on each input line and outputs the");
      printf("\ntotal number of characters and lines entered.");
      printf("\Terminate each line with Enter. Terminate the");
      printf("\nprogram by pressing Enter [Ctrl]+Z, and Enter.\n\n");
      
      while((ch = getchar()) != EOF)
      {
         num_lines++;
         do
         {
           num_chars++;         
         }while((ch = getchar()) != '\n');
    
      }
      
      printf("\You entered %d characters and %d lines.\n\n",num_chars,num_lines);
      
      system("PAUSE");	
      return 0;
    }
    I just can't figure out how to do it

    Excuse the system call I know I'm suppose to use getch()

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,336
    This is not a free coding factory. Provide info about your attempt, and people will be more likely to help.

    Incidentally, getch() is not standard C either - so you're not supposed to use it. It's a philosophical argument whether it's better to use system("pause") (which has implementation-defined behaviour, so is not guaranteed to do anything in particular), or getch() (which is not standard, so is not guaranteed to be supported by your compiler/library). There is plenty of information available about alternative approaches, some of which even make use of standard functions for which behaviour is well specified.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    Sorry bout that I guess I should say why isn't this code working or I can't get it to work it doesn't have any errors in it. It just doesn't work here's my code that I have.
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Chapter5Problem6
    {
        class Program
        {
            static void Main(string[] args)
            {
                string ch;
                int num_chars = 0;
                int num_lines = 0;
    
                Console.Write("\nThis program counts the number of characters");
                Console.Write("\nyou enter on each input line and outputs the");
                Console.Write("\ntotal number of characters and lines entered.");
                Console.Write("\nTerminate each line with Enter. Terminate the");
                Console.Write("\nprogram by pressing Enter [Ctrl]+Z, and Enter.\n\n");
    
                while ((ch = Console.ReadLine()) != null)
                {
                    num_lines++;
                    do
                    {
                        num_chars++;
                    } while ((ch = Console.ReadLine()) != "\n");
                }
    
                Console.Write("\nYou entered {0} characters and {1} lines.\n\n", num_chars, num_lines);
    
    
                Console.ReadKey();
            }
        }
    }
    Sorry about the code not formatting right I couldn't fix it I hope you guys can read it
    Last edited by artistunknown; 03-14-2014 at 07:00 PM.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    disregard My last statement I guess the format is correct

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    Quote Originally Posted by artistunknown View Post
    disregard My last statement I guess the format is correct
    theres no errors its when I hit ctrl z nothing happens it doesn't end the program like its suppose to and print the # of lines and characters

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,336
    You might want to start by reading the documentation for Console.ReadLine(), rather than assuming it behaves like C's getchar() function. It does not include the end of line markers in the string it returns, and it does not read one character at a time. Your code assumes it does both. If the first call of Console.ReadLine() doesn't return null, the inner loop will run forever.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    I see what you mean by Console.ReadLine() not being like getch() there nothing alike I read a little on ReadLine() and it was somewhat discouraging because I don't have the background to understand it that deep some of those technical docs gets a little confusing so therefore my code is somewhat useless. I tried to approach it the same as the c source. I'll probably skip this problem and move on to something new sorry for being so illogical I just thought that someone would be able to translate it for me easily probably shouldn't have made that assumption

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,336
    Aww.... cmon. YOU actually need to apply effort.

    You're essentially claiming you're incapable of converting code that uses a function that reads one character at a time (getchar()) so it uses a function that reads a line at a time and discards newlines (Console.GetLine()).

    If that reflects your ability, it's a wonder you know how to turn a computer on. If it reflects your effort (most likely, as most people have the ability but need to TRY) you and the world would be better off if you abandoned software and got a job flipping burgers (although it does take effort and technique to flip burgers, so maybe you need to find something less demanding to do).
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    You got problems bud

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    Sorry to insult you grumpy. I know your just trying to help. I just feel like the problem is a little over my head. I know I should probably try a little harder. I just think text input in c and c# are too different for my newbie butt hee hee

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    Well grumpy motivated me to figure this out so I came up with this its closer but, I'm still not there

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Chapter5Problem6
    {
        class Program
        {
            static void Main()
            {
                int ch;
                int num_chars = 0;
                int num_lines = 0;
    
    
                while ((ch = Console.Read()) != 0 )
                {
                    num_lines++;
                    do
                    {
                        num_chars++;
                    }while((ch = Console.Read()) != '\n');
                }
    
                Console.Write("\nYou entered {0} characters and {1} lines.\n\n", num_chars, num_lines);
                Console.ReadKey();
    
            }
        }
    }
    Last edited by artistunknown; 03-15-2014 at 10:46 AM.

  12. #12
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    well I know this code isn't right but I'm on the right track I think

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Chapter5Problem6
    {
        class Program
        {
            static void Main()
            {
                int ch;
                int num_chars = 0;
                int num_lines = 0;
    
    
                while ((ch = Console.Read()) != 0 )
                {
                    num_lines++;
                    if (ch == 'z')
                        break;
                    else
                        num_chars++;
                }
    
                Console.Write("\nYou entered {0} characters and {1} lines.\n\n", num_chars, num_lines);
                Console.ReadKey();
    
            }
        }
    }

  13. #13
    Registered User
    Join Date
    Mar 2014
    Posts
    4
    Code:
    static void Main(string[] args)
            {
                string ch;
                int num_chars = 0;
                int num_lines = 0;
               
    
    
                Console.Write("\nThis program counts the number of characters");
                Console.Write("\nyou enter on each input line and outputs the");
                Console.Write("\ntotal number of characters and lines entered.");
                Console.Write("\nTerminate each line with Enter. Terminate the");
                Console.Write("\nprogram by pressing Enter [Ctrl]+Z, and Enter.\n\n");
    
    
                do
                {
                    ch = Console.ReadLine();
                    if (ch != null)
                    {
                        num_lines++;
                        foreach (char c in ch)
                        {
                            num_chars++;
                        }
                    }
                } while (ch != null);
    
    
                Console.Write("\nYou entered {0} characters and {1} lines.\n\n", num_chars, num_lines);
            }
    Hope that helps

  14. #14
    Registered User
    Join Date
    Jan 2010
    Posts
    150
    Hey man! Thanks a bunch. Because I was struggling with the problem I would have never thought of using that code

  15. #15
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    In general I recommend against blindly porting code from one language to another, at least until you've achieved mastery in both languages. It tends to produce code that is similar in quality to written text, translated to Chinese and then back to English.

    Don't think about "what's the C# equivalent to X", think "how do I accomplish Y in C#". The C# code may look little to nothing like the C code, and that's fine. The goal isn't to solve the problem in exactly the same manner.

    For the record, this post translated into Chinese then back to English twice becomes:
    Overall, I do not recommend blind portable code from one language to another, at least until you have reached mastered both languages. It tends to produce code that is similar in quality written text, translated into Chinese and then back to English.

    Do not think, "What is the equivalent to X in C#", that "how I implement in C# Y." C# code might look almost nothing like C code, which is good. The purpose is not to solve the problem in exactly the same way.
    stahta01 likes this.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert gsoap PHP program to C
    By straygrey in forum C Programming
    Replies: 20
    Last Post: 10-31-2013, 08:33 AM
  2. Replies: 1
    Last Post: 03-16-2010, 10:17 AM
  3. Convert c++ program to c
    By tnt666 in forum C Programming
    Replies: 7
    Last Post: 12-01-2008, 08:23 AM
  4. Convert a C++ program?
    By unregistered in forum C Programming
    Replies: 4
    Last Post: 05-22-2002, 02:44 AM
  5. Can anybody convert this program.
    By andy bee in forum C Programming
    Replies: 3
    Last Post: 08-28-2001, 05:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21