Click here to Skip to main content
15,891,938 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
using System;
using System.Collections;
using System.Collections.Generic;

class MainClass {
  public static void Main (string[] args) {
    Console.WriteLine ("Gimme some values to add to key and value");
    Console.Write("Key: ");
    string a = Console.ReadLine();
    Console.Write("Value: ");
    string b = Console.ReadLine();
    var anurag = new Dictionary<string, string="">();
    anurag.Add(a, b);
  while(true){
    Console.WriteLine("Do you want to add another entry?");
    string c = Console.ReadLine();
    
    if(c.ToLower()=="yes"){
      Console.Write("Write Name: ");
      string d = Console.ReadLine();
      Console.Write("Write Value: ");
      string e = Console.ReadLine();
      anurag.Add(d, e);
      Console.WriteLine("Do to want to view the whole list or add another person?");
      string f = Console.ReadLine();
      if (f.ToLower()=="view"){
        Console.WriteLine("All the people");   
  
        foreach (KeyValuePair<string, string=""> person in anurag)  
       {  
        Console.WriteLine("Key: {0}, Value: {1}",  
            person.Key, person.Value);  
       }  

      }else if(f.ToLower()=="add"){
        
        continue;
      }
    }
    else{
      //this part of the program wont work.. is it because i put "continue;"? If yes, how do i fix it? and also it says error on (42, 33) CS1501 No overload for method'ReadLine' also how do i loop back this part of the code so that after this part of code is over it comes back to ask if they want to add another name or not. Also while you are at it, pls suggest me ways to improve my code... Thanks :)
      Console.WriteLine("Do to want to remove or edit a person?");
      string remOrEdit= Console.ReadLine("");
      if(remOrEdit.ToLower()=="Edit"){
        Console.Write("Enter the name of the person you want to edit: ");
        string FrEdit = Console.ReadLine();
        Console.Write("Change the value to? ");
        string ValEdit = Console.ReadLine();
        anurag[FrEdit]=ValEdit;
      }
      }
    }
   }
  }<pre>

What I have tried:

I dunno what to try my program wont run for the error mentioned in line 41 so i dunno what to try.. If that error wasnt there trust me i wouldve tried my best to resolve the issue...
Posted
Updated 3-Sep-20 3:05am
v2

1. Always divide your code such that repetitive part is a method written once and can be called when needed.
Asking for key-value and adding to dictionary in your case

2. You should be explicit on response type when you ask a query. Like (Y/N or yes/no along with query)
Do you want to add another entry?

3. Always use meaningful variable names for easy readability and context
var anurag = new Dictionary<string, string= "" > ();

4. Segregate the responsibilities into specific methods
Having add, view & delete of the values in one place confuses the sequence and makes it difficult to read


Now, with your code:
1. Incorrect syntax
C#
string remOrEdit = Console.ReadLine("");

Should be:
C#
string remOrEdit = Console.ReadLine();

2. Exit of while loop
C#
while(true)

You explicitly used true as condition which will never be false thus an infinite loop app. Your continue though takes other iteration of while. Thus, your else part there is never executed.

Try to break it up and have clear sequence of things to have easy handle of your operations.
 
Share this answer
 
C#
string remOrEdit= Console.ReadLine("");

The ReadLine[^] method does not take a string as a parameter.
C#
if(remOrEdit.ToLower()=="Edit"){

Why are you using a capital letter in the string that you are trying to compare with all lowercase?

There may be other problems that I did not notice.
 
Share this answer
 
Just to add to what Richard has said:
Your loop has no way to exit:
C#
while(true){
Somewhere in that loop, you need a break; to exit the loop, or a return; to exit the method.

I'd also strongly suggest you indent your code properly: it makes it a lot easier to read and thus understand and maintain. Visual Studio will do that for you once you get rid of compiler errors, just press "CTRL+K D" and it will format your document according to your chosen indentation style.

When you get rid of the compiler errors, look to use the debugger - it'll help you work out exactly what is going on while your code is running - Google for "Visual Studio Debugger" and you'll find loads of help on how to use it. It really is your bestest friend, so get used to it - we all spend a lot more time there than in the code editor!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900